jdbc connection leak

435 views
Skip to first unread message

Todd Tucker

unread,
Mar 16, 2014, 2:03:50 PM3/16/14
to activejd...@googlegroups.com
I know this is probably a question for another forum but I wanted to make sure I'm not missing something with my ActiveJdbc setup

I get the following SQL Exception when trying to open a DB Connection first thing every morning:
            Could not close connection! MUST INVESTIGATE POTENTIAL CONNECTION LEAK! java.sql.SQLException: Already closed.

It throws this exception 3 times before working on the 4th attempt. After that it works fine the rest of the day. I'm using Mysql (Actually MariaDb 5.5) and tomcat 7 connection pooling. 
From what I've looked at everything seems to be setup correctly for pooling and works except after what looks to be a period of inactivity.  So for some reason I guess the connections are not getting cleaned up. I'm using the sample ActiveJdbcFileter with a couple modifications. Below is the stack trace:

2014-03-16 11:33:46  WARN [http-bio-8080-exec-5] (DB.java:224) - Could not close connection! MUST INVESTIGATE POTENTIAL CONNECTION LEAK!
java.sql.SQLException: Already closed.
        at org.apache.tomcat.dbcp.dbcp.PoolableConnection.close(PoolableConnection.java:114)
        at org.apache.tomcat.dbcp.dbcp.PoolingDataSource$PoolGuardConnectionWrapper.close(PoolingDataSource.java:191)
        at org.javalite.activejdbc.DB.close(DB.java:221)
        at org.javalite.activejdbc.Base.close(Base.java:123)
        at com.creditsys.filter.ActiveJdbcWebFilter.doFilter(ActiveJdbcWebFilter.java:75)
        .....

And this is my context.xml with the jdbc configs:

<Context antiJARLocking="true" path="/">

    <Resource name="jdbc/creditsys" auth="Container" type="javax.sql.DataSource"
          maxActive="100" maxIdle="30" maxWait="10000" 
          username="xxxxx" password="xxxxxx" driverClassName="com.mysql.jdbc.Driver"
          url="jdbc:mysql://localhost:3306/creditsys"/>

</Context>

And the relevant parts of ActiveJdbcWebFiler.java

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    
        jndiName = filterConfig.getInitParameter("jndiName");
    .....
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

    try{
            Context initCtx = new InitialContext();
            Context envCtx = (Context) initCtx.lookup("java:comp/env");

            DataSource ds = (DataSource) envCtx.lookup(jndiName);

            Base.open(ds);
            Base.openTransaction();

            chain.doFilter(request, response);

            Base.commitTransaction();
     } ....

     finally {
            Base.close();
     }


   

igor

unread,
Mar 16, 2014, 8:48:20 PM3/16/14
to activejd...@googlegroups.com
Todd, looks like your setup is correct (I assume that your web.xml passes correct  jndiName to the filter). 

You can try setting up the same setup without ActiveJDBC to see results. We are using ActiveWeb on Tomcat 7 and Maria DB as well. Here is configuration of the pool just in case: 

 <Resource name="jdbc/pigeon" 
     auth="Container" 
     type="javax.sql.DataSource" 
     factory="org.apache.tomcat.jdbc.pool.DataSourceFactory"
     
     testWhileIdle="true"
     testOnBorrow="false"
     testOnReturn="false"
     validationQuery="SELECT 1"
     validationInterval="30000"
     timeBetweenEvictionRunsMillis="30000"
     removeAbandoned="false"
     logAbandoned="false"
     minEvictableIdleTimeMillis="30000" 
     
     initialSize="10"      
     maxActive="50" 
     minIdle="10" 
     maxIdle="25"
     maxWait="30000"      
     
     jmxEnabled="true"
     jdbcInterceptors="org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer;org.apache.tomcat.jdbc.pool.interceptor.ResetAbandonedTimer"
     defaultTransactionIsolation="READ_COMMITTED"
     
     username="*******" 
     password="*******" 
     driverClassName="com.mysql.jdbc.Driver"

I hope it helps

tanks
igor

Todd Tucker

unread,
Mar 17, 2014, 3:34:05 PM3/17/14
to activejd...@googlegroups.com
Great, thanks Igor. 

One thing I just found and fixed was that when I set the factory parameter (which I hadn't but it still worked even though the docs state it's required) it wouldn't connect at all and gave a NamingException, which was fixed by moving the mysql-connector-java.jar from my local webapp/lib into tomcat's global lib, $CATALINA_BASE/lib.  I'll see if that fixes it but if not I'll test out some of these other parameters. 

Igor Polevoy

unread,
Mar 17, 2014, 5:54:27 PM3/17/14
to activejd...@googlegroups.com
yes the MySQL driver jar needs to be installed at the container level
tx
Reply all
Reply to author
Forward
0 new messages