I have a weird problem where two web apps with Oracle JDBC driver will conflict with each other. I have to put the JDBC driver JAR in the common folder TOMCAT_HOME/lib. What is the reason for this?
1 Answers
JDBC drivers register themselves in the JVM-wide singleton DriverManager
which is shared by all web apps. If you have the same (as in class name) JDBC driver register twice from two different web apps, this might cause your problem. This is even more problematic if your web apps use different versions of the same JDBC driver.
Also, putting JDBC drivers into Tomcat's lib folder will help prevent memory leaks when you redeploy your web app without restarting Tomcat, e.g. if you just put a new WAR file into Tomcat's webapps folder:
The class DriverManager
gets loaded by the bootstrap classloader and thereby "lives" globally in the JVM, while Tomcat loads all web apps in their own classloaders. So if a JDBC driver from a web app's WEB-INF/lib folder registers itself in DriverManager
, it pins that web app's classloader in memory (and thereby all the classes of that web app), preventing its garbage collection.
If instead both DriverManager
and JDBC drivers come from non-web app classloaders, you can freely redeploy your web apps without any web app classes pinning themselves in classes loaded from other classloaders.
Current versions of Tomcat (probably 6.x and definitely 7.x) will log warnings on undeployment of a web app if a memory leak is detected, among other things by JDBC drivers.

- 20,771
- 6
- 58
- 65
-
placing the sql jar inside lib folder of tomcat "apache-tomcat-7.0.37\lib" will solve the issue? – greenhorn Apr 23 '15 at 06:33
-
1Yes, if you put a single JDBC driver JAR into Tomcat's lib/ (instead of your web app's lib/), this will avoid conflicts like described by the original poster. Of course, all your apps have to be able to work with the same JDBC driver version then. – Philipp Reichart Apr 23 '15 at 17:34
-
26Is it just me, or does a "JVM-wide singleton DriverManager" seem like a major bug in the design of JDBC? – James Newman Sep 28 '15 at 19:57
-
1@PhilippReichart is this still a reality in most recent versions of the plugin? I have a similar issue https://stackoverflow.com/questions/45621073/where-to-put-c3p0-dependency-in-tomcat-container and am struggling to know the correct place to put the postgres driver in - especially in the case of using different versions of the driver for different applications. – mmalmeida Aug 14 '17 at 16:49
-
I placed my SQL jar inside the tomcat lib, and changed my dependency in maven to `provided`, and I'm still getting this error – Ammar Samater Mar 13 '18 at 12:48
-
is TOMCAT_HOME/lib better than TOMCAT_HOME/shared? What is the best practice? – David Brossard Apr 13 '18 at 12:49
-
1@DavidBrossard There is no "shared" folder by default any more in recent versions of Tomcat such as 9. By default, the "Server", "Shared", and "Common" class loaders all load classes and jars from the same pair of places: a `lib` folder found in Tomcat/Catalina "home" folder, and a `lib` folder in Tomcat/Catalina "base" folder. See the `catalina.properties` file for explanations in the comments, and edit that file if you want a different configuration. – Basil Bourque Oct 15 '19 at 23:57
-
Is this issue the same when using different tomcats and users but the same java installation on a unix server? So basically 2 different jvm instances? – Pwnstar Nov 28 '19 at 10:29