为什么必须将JDBC驱动程序放在TOMCAT_HOME/lib文件夹中? [英] Why must the JDBC driver be put in TOMCAT_HOME/lib folder?

查看:535
本文介绍了为什么必须将JDBC驱动程序放在TOMCAT_HOME/lib文件夹中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题,两个带有Oracle JDBC驱动程序的Web应用程序将相互冲突.我必须将JDBC驱动程序JAR放在公用文件夹 TOMCAT_HOME/lib 中.是什么原因呢?

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?

推荐答案

JDBC驱动程序在JVM范围内的单例中注册自己

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.

此外,将JDBC驱动程序放入Tomcat的lib文件夹将有助于防止在重新部署Web应用程序而无需重新启动Tomcat时发生内存泄漏,例如.如果您只是将新的WAR文件放入Tomcat的webapps文件夹中:

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:

DriverManager由引导类加载器加载,从而在JVM中全局驻留",而Tomcat在其自己的类加载器中加载所有Web应用程序.因此,如果Web应用程序的WEB-INF/lib文件夹中的JDBC驱动程序在DriverManager中进行了自身注册,则它将该Web应用程序的类加载器固定在内存中(从而锁定该Web应用程序的所有类),从而防止了其垃圾回收.

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.

如果DriverManager和JDBC驱动程序均来自非Web应用程序类加载器,则您可以自由地重新部署Web应用程序,而无需任何Web应用程序类将自己固定在从其他类加载器加载的类中.

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.

当前版本的Tomcat(可能是6.x,最好是7.x)将在未部署Web应用程序时记录警告,如果检测到内存泄漏,则由JDBC驱动程序进行检测.

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.

这篇关于为什么必须将JDBC驱动程序放在TOMCAT_HOME/lib文件夹中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆