c3p0 Connection pool memory leak redeploy tomcat
Asked Answered
O

1

6

I have these codes for closing c3p0 connection manager but it seems that there is still one more thread that was not closed . Am i missing something ?

Oct 11, 2016 5:12:09 PM org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing WebApplicationContext for namespace 'dispatcherServlet-servlet': startup date [Tue Oct 11 17:11:58 PHT 2016]; parent: Root WebApplicationContext
2016-10-11 17:12:09 - [INFO ] CRMContextListener - Trying to Close
2016-10-11 17:12:09 - [INFO ] CRMContextListener - Close Success
Oct 11, 2016 5:12:09 PM      org.springframework.context.support.AbstractApplicationContext doClose
INFO: Closing Root WebApplicationContext: startup date [Tue Oct 11 17:11:44 PHT 2016]; root of context hierarchy
Oct 11, 2016 5:12:09 PM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SEVERE: A web application registered the JBDC driver [oracle.jdbc.OracleDriver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered. Oct 11, 2016 5:12:09 PM     org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: A web application appears to have started a thread named [C3P0PooledConnectionPoolManager[identityToken->2xjsis9j1do3mrp1m3tqb8|fb6fd9c]-DeferredStatementDestroyerThread-#0] but has failed to stop it. This is very likely to create a memory leak.

This is the code for my servlet listener

public void contextDestroyed(ServletContextEvent sce) {
    logger.info("Trying to Close");

    for (Object o : C3P0Registry.getPooledDataSources()) {
        try {
            ((PooledDataSource) o).close();
        } catch (Exception e) {
            logger.info("No thread was open...");
        }
    }

    logger.info("Close Success");
}

And here is my configuration for c3p0 xml

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
    <property name="driverClass" value="oracle.jdbc.driver.OracleDriver" />
    <property name="jdbcUrl" value="jdbc:oracle:thin:@sph-pdc-vm  1042:1521:DEV" />
    <property name="user" value="TSW" />
    <property name="password" value="TSW2015#" />
    <property name="minPoolSize" value="2" />
    <property name="maxPoolSize" value="20" />
    <property name="initialPoolSize" value="5" />
    <property name="testConnectionOnCheckin" value="true" />
    <property name="idleConnectionTestPeriod" value="100" />
    <property name="maxIdleTimeExcessConnections" value="5" />
    <property name="maxStatementsPerConnection" value="10" />
    <property name="acquireIncrement" value="1" />
    <property name="statementCacheNumDeferredCloseThreads" value="1" />
    <property name="acquireRetryAttempts" value="2" />
    <property name="acquireRetryDelay" value="2000" />
</bean>

Thanks in advance....

Orren answered 12/10, 2016 at 0:11 Comment(1)
The Pool data source cleanup indeed helps. Thank you so much! for (Object o : C3P0Registry.getPooledDataSources()) { try { ((PooledDataSource) o).close(); } catch (Exception e) { logger.info("No thread was open..."); } }Levkas
A
2

It's likely not to be a real memory leak, but a miswarning because c3p0's Threads take some time to wind down, and close() does not wait for that to complete, it's asynchronous. So you have an annoying message. See this github issue, thanks to Buğra Gedik on github. If you want to test this theory, you might add a delay of a second or so [i.e. call Thread.sleep(1000)] after your call to close() and see if the message goes away.

Although I don't think this is your issue, you might consider also adding some Tomcat parameters to be sure that attempts to close() the Threads always succeed. Just add...

<property name="privilegeSpawnedThreads" value="true" />
<property name="contextClassLoaderSource" value="library" />

See c3p0's docs.

Agraffe answered 12/10, 2016 at 2:10 Comment(3)
I tried calling the Thread.sleep(1000) after the close(). But the message is still there.Orren
This actually solves the issue , your theory is correct !!! I just increase the time delay to 3000 and the log for memory leak disappears ! Thanks !Orren
additional question . does undeploying application in tomcat should decrease the perm gen space ? because when i try to undeploy my application. the perm gen increase by 2mb. and when i redeploy my application . it increases by 20mb.Orren

© 2022 - 2024 — McMap. All rights reserved.