user3793589
user3793589

Reputation: 418

Suspected memory leak with Jdbc and Tomcat

I am having theses messages in my tomcat logs :

" org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc A web application 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."

and

"org.apache.catalina.loader.WebappClassLoader clearReferencesThreads SEVERE: A web application appears to have started a thread named [pool-820-thread-1] but has failed to stop it. This is very likely to create a memory leak."

Actually I have a JDBC driver (.jar) in my java project that I always deploy on a tomcat server as a .war (meaning the driver is always in the war/libs directory).

After my searches, I found a good starting of answer here but unfortunately I cannot comment yet on stackoverflow to have more details on the accepted answer.

Here are my questions : - Is the answer suggesting to remove completely the .jar from the war/libs directory ? - If yes, where do I put it? Because I do not get how to get rid completely of the .jar and still be able to make my tests locally to the database.

Please advice on this.

Upvotes: 0

Views: 2341

Answers (1)

Sumeet Sharma
Sumeet Sharma

Reputation: 2583

Since Tomcat 6.0 , there has been a feature to detect classloader memory leaks. Read more here. The above messages by tomcat are purely for information purpose and tomcat already took sufficient measures to avoid classloader leak by de-registering the Driver.

To prevent it as you have rightly pointed out , you can either move the jar completely to tomcat's lib folder where it wont be affected by a application context reload. Or you can explicitly call a DriverManager.deregister(driver). (Read here)

To understand more about ClassLoader leaks (here)

To understand why its suggested to move it to tomcat lib from applications WEB_INF/lib you can read more here.

Edit in response to query in the comment

No its not recommended to have multiple jars as it can lead to classcastexception. Each class is identified by combination of class name and classloader. Look at this for a clearer understanding with an example.

The better approach according to me would be to write a servlet context listener and explicitly de-register your driver at context destroyed. This way you can keep you jdbc driver in your web-inf/lib and need not move it to tomcat/lib.

Even if you keep jars at multiple locations, according to the classloader hierarchy that tomcat follows which is different from what java delegation model (more info here) , tomcat will pick up the jar in your web-inf/lib first.

Upvotes: 2

Related Questions