File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes jdbc connections not closing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "jdbc connections not closing " Watch "jdbc connections not closing " New topic
Author

jdbc connections not closing

David Alt
Greenhorn

Joined: Jan 29, 2011
Posts: 3
I'm stumped with this one:

I’m using the following:
Hibernate 3.5 with c3p0
Struts2
Tomcat 6.0.29
Java 6

I get an error indicating a memory leak whenever the webapp is reloaded without shutting down Tomcat completely. For example, I see the following when the webapp reloads in Eclipse:

Jan 29, 2011 11:20:50 AM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
SEVERE: The web application [/MyApp] registered the JBDC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.

Then I'll eventually see:

Exception in thread "Timer-0" java.lang.NullPointerException
at com.mchange.v2.log.log4j.Log4jMLog$Log4jMLogger.isLoggable(Log4jMLog.java:255)
at com.mchange.v2.resourcepool.BasicResourcePool$CullTask.run(BasicResourcePool.java:1934)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)

If I keep allowing the app to reload, I eventually run out of permagen memory.

When I reload the application using Tomcat Manager it fails to close JDBC connections, and they'll start to multiply.

Is there a way to ensure that all connections are closed and the driver unregistered whenever the application reloads?

My Hibernate configurations are shown below ...

Thanks for any help!

David

config.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");

// Read in from properties file:
config.setProperty("hibernate.connection.url",
WebAppProperties.getProperty("db.host"));
config.setProperty("hibernate.connection.username",
WebAppProperties.getProperty("db.username"));
config.setProperty("hibernate.connection.password",
WebAppProperties.getProperty("db.password"));


config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");

config.setProperty("hibernate.connection.provider_class", "org.hibernate.connection.C3P0ConnectionProvider");
config.setProperty("hibernate.c3p0.min_size", "5");
config.setProperty("hibernate.c3p0.max_size", "20");
config.setProperty("hibernate.c3p0.timeout", "60");
config.setProperty("hibernate.c3p0.max_statements", "50");
config.setProperty("hibernate.c3p0.idle_test_period", "3000");

config.setProperty("hibernate.current_session_context_class", "thread");
config.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.NoCacheProvider");

config.setProperty("hibernate.hbm2ddl.auto",
WebAppProperties.getProperty("db.hbm2ddl"));
Jan Cumps
Bartender

Joined: Dec 20, 2006
Posts: 2495
    
    8

Welcome to the Ranch, David!


OCUP UML fundamental and ITIL foundation
youtube channel
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

I'm not sure the two issues are related to the final premgen error but I'll proceed.

The first issue is related to the DB driver being loaded by the webapp and not removing it when the webapp stops. This is not related to closing a particular connection, it is just the Driver. You can implement a ContextListener to remove the Driver but personally it has never caused me any issues.

For the second, calls to Log4J are thread safe, but you can still get strange errors when using Log4J in multi threaded environments. One cause for this is that another thread results in a logging call but the logging includes a Class that was unloaded by the container during the shutdown or redeploy. A suggested solution is to use Apache commons-logging or slf4j to abstract the logging behaviour but I haven't tried it explicitly and not 100% it works.

As for the last part, the actual premgen error, sometimes that can just be a case of not having enough pergen space allocated, but in your case does sound like a memory leak. I have seen the same behaviour using Liferay 5.2 but not in Liferay 6 (in case you're using this) but other than that you could look at the Eclipse based Memory Analysis Tool to grab a memory dump and see if you can find the problem.

and Welcome to the Ranch
David Alt
Greenhorn

Joined: Jan 29, 2011
Posts: 3
Yes, there's definitely a memory leak when it reloads in Eclipse. I get the following:

SEVERE: The web application [/MyApp] registered the JBDC driver [com.mysql.jdbc.Driver] but failed to unregister it when the web application was stopped. To prevent a memory leak, the JDBC Driver has been forcibly unregistered.
Jan 30, 2011 8:42:51 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/MyApp] appears to have started a thread named [Timer-0] but has failed to stop it. This is very likely to create a memory leak.
Jan 30, 2011 8:42:51 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/MyApp] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#0] but has failed to stop it. This is very likely to create a memory leak.
Jan 30, 2011 8:42:51 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/MyApp] appears to have started a thread named [com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread-#1] but has failed to stop it. This is very likely to create a memory leak.
Jan 30, 2011 8:42:51 PM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads

Tomcat Manager also shows that there has been a memory leak upon redeploy.

-David
Andy McCann
Greenhorn

Joined: Mar 03, 2011
Posts: 2
I'm experiencing exactly the same issue, was any solution found here?
David Alt
Greenhorn

Joined: Jan 29, 2011
Posts: 3
I don't have one yet. I haven't had a chance to dig into it with a profiler, and so I just restart the server whenever I release. Sorry...
David Zonsheine
Greenhorn

Joined: Mar 16, 2011
Posts: 1
Any updates on this?

We have a very small Tomcat 6.0.32, Spring 3.0.3, Hibernate 3.2.6, c3p0 0.9.1.2 configuration.

Datasources are configured to shutdown when Spring goes down.

We are sure that the host is not leaking as it loads and free huge amount of data for weeks with no change in memory usage.

Still, when shut down we see the trace above for the potential memory leak.

Thanks,

David Zonsheine
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: jdbc connections not closing