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

查看:30
本文介绍了为什么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 范围的单例中注册自己 DriverManager所有网络应用程序共享.如果您从两个不同的 Web 应用程序中注册了相同的(如类名)JDBC 驱动程序两次,这可能会导致您的问题.如果您的 Web 应用程序使用同一 JDBC 驱动程序的不同版本,这会更成问题.

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天全站免登陆