tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jakarta Tomcat Newsgroup (@Basebeans.com) <jakarta-...@basebeans.com>
Subject Re: Commons DBCP and closing connections
Date Sat, 24 Aug 2002 03:10:06 GMT
Subject: Re: Commons DBCP and closing connections
From: Matt Raible <matt@raibledesigns.com>
 ===
I think the problem is that it's not creating a "pooled" connection.  When
you are using a connection pool, con.close() should just return it, not
actually close it - right?

In my opening method, I'm getting "Non-Pooled Connection" each time.

    /** 
     * Retrieves a connection from the connection pool
     */
    public Connection getPooledConnection() throws ServiceLocatorException
    {
        try {
            ds = (DataSource) getEnvContext().lookup(Constants.JNDI_DB);
        } catch (NamingException ex) {
            logger.error("NamingException: " + ex.getMessage());
            throw new ServiceLocatorException(ex);
        }
        
        try {
            if (ds instanceof ConnectionPoolDataSource) {
                ConnectionPoolDataSource poolDataSrc =
(ConnectionPoolDataSource) ds;
                PooledConnection pc = poolDataSrc.getPooledConnection();
                con = pc.getConnection();
                if (logger.isDebugEnabled()) {
                    logger.debug("Pooled Connection");
                }
            } else {
                if (logger.isDebugEnabled()) {
                    logger.debug("Non-Pooled Connection");
                }
                con = ds.getConnection();
            }
        } catch (SQLException ex) {
            logger.error("SQLException: " + ex.getMessage());
            throw new ServiceLocatorException(ex);
        }
        return con;
    }

On 8/23/02 8:56 PM, in article
mailman.1030157820.16927.jakarta_tomcat@basebeans.com, "Peter Davison"
<peterdavison@rogers.com> wrote:

> Correct me if I'm wrong but if the first con.close() call succeeds your code
> will still execute the second con.close() call in the finally block won't it?
> 
> Closing the connection won't necessarily dereference the "con" variable, so
> it's
> trying to close a connection that is already closed, which would explain the
> exception you're getting.
> 
> P.
> 
> On Fri, 23 Aug 2002 19:10:09 -0700
> Jakarta Tomcat Newsgroup (@Basebeans.com) <jakarta-tom@basebeans.com> wrote:
> 
>> Subject: Commons DBCP and closing connections
>> From: Matt Raible <matt@raibledesigns.com>
>>  ===
>> I am trying to upgrade from using Tyrex 0.9.7 to DBCP (from Struts 1.1 b2) -
>> and I have the following method that used to work fine:
>> 
>>     /** Closes a connection from the connection pool */
>>     public void closeConnection(Connection con) throws
>> ServiceLocatorException
>>     {
>>         try {
>>             con.close();
>>         } catch (SQLException sqle) {
>>             logger.error("SQLException: " + sqle.getMessage());
>>             throw new ServiceLocatorException(sqle);
>>         } finally {
>>             if (con != null) {
>>                 // try again
>>                 try {
>>                     con.close();
>>                 } catch (SQLException csqle) {
>>                     // ignore
>>                 }
>>             }
>>         }
>>     }
>> 
>> But now it causes a "connection closed" error??  I thought when you were
>> using a connection pool, "closing" the connection just releases it back to
>> the pool.  Am I doing this right??
>> 
>> Matt
>> 
>> 
>> --
>> To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
>> For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>
>> 
> 
> 
> --
> Peter Davison
> peterdavison@rogers.com
> 
> Don't everyone thank me at once!
> -- Han Solo
> 
> --
> To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>
> 


--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


Mime
View raw message