重新部署后Tomcat webapp无法使用MySQL驱动程序
我正在使用 commons-dbcp 在一个小型 Spring web 应用程序中为 MySQL 数据库进行连接池,该应用程序使用 Sun 1.6 JDK 作为 WAR 文件部署到本地 Tomcat 6.0.28 容器(使用 Ubuntu 包管理器安装)。部署是使用 tomcat-maven-plugin-1.1 从 Maven 完成的,目标是 tomcat:redeploy
。 MySQL Connector/J jar 位于 web 应用程序的 WEB-INF/lib 目录中。
Tomcat 启动后第一次加载 web 应用程序时,一切正常。但是,当我重新部署 Web 应用程序时,当 Tomcat 取消部署 Web 应用程序时,它会抱怨 JDBC 驱动程序未注册。
code>SEVERE: The web application [/taskrun] 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. Mar 11, 2011 11:29:46 AM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
然后,当 Tomcat 重新启动 Web 应用程序时,它无法连接到数据库,并抱怨没有合适的驱动程序:
org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.jdbc2.optional.MysqlDataSource' for connect URL 'jdbc:mysql://127.0.0.1:3306/testdb?autoReconnect=true'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
... 14 more
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:279)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
... 18 more
如果我在重新部署后手动重新启动 Tomcat,则一切正常。
我知道 MySQL 驱动程序在加载类时向 DriverManager 注册自身,但是当重新部署 web 应用程序时,com.mysql.jdbc.Driver
类似乎没有重新加载。重新加载 Web 应用程序时,Tomcat 的 WebappClassLoader 是否应该从 WEB-INF/lib 目录重新加载类,或者是否在部署之间将它们保留在内存中?或者部署后我必须自己重新注册 MySQL 驱动程序吗?
另外,我看到很多帖子建议将驱动程序 jar 移动到 Tomcat 的 commons/lib 目录,但我想尽可能避免 WAR 文件外部的依赖项。
预先感谢您提供的任何帮助。
I am using commons-dbcp to do connection pooling for a MySQL database in a small Spring webapp which is deployed as a WAR file to a local Tomcat 6.0.28 container (installed using the Ubuntu package manager) using the Sun 1.6 JDK. The deploy is done from Maven using tomcat-maven-plugin-1.1 with the goal tomcat:redeploy
. The MySQL Connector/J jar is in the webapp's WEB-INF/lib
directory.
The first time the webapp is loaded after Tomcat starts, everything works fine. However, when I redeploy the webapp, as Tomcat undeploys the webapp it complains that the JDBC driver was not unregistered.
code>SEVERE: The web application [/taskrun] 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. Mar 11, 2011 11:29:46 AM org.apache.catalina.loader.WebappClassLoader clearReferencesJdbc
Then, when Tomcat restarts the webapp, it fails to connect to the database, complaining about no suitable driver:
org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'com.mysql.jdbc.jdbc2.optional.MysqlDataSource' for connect URL 'jdbc:mysql://127.0.0.1:3306/testdb?autoReconnect=true'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1452)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:79)
... 14 more
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:279)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1437)
... 18 more
Everything works fine if I manually restart Tomcat after the redeploy.
I know the MySQL driver registers itself with the DriverManager when the class is loaded, but the com.mysql.jdbc.Driver
class doesn't seem to be reloaded when the webapp is redeployed. Is Tomcat's WebappClassLoader supposed to reload classes from the WEB-INF/lib
directory when the webapp is reloaded or does it keep them in memory between deploys? Or do I have to re-register the MySQL driver myself after a deploy?
Also, I've seen plenty of posts suggesting moving the driver jar to Tomcat's commons/lib
directory, but I would like to avoid dependencies external to my WAR file as much as possible.
Thank you in advance for any assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 JDBC 驱动程序放置到 WEB-INF/lib 通常是个坏主意。通常,JDBC 驱动程序在所有已部署的应用程序之间共享,并且在正确的配置中,它们应该是全局 Tomcat 配置的一部分(将其放入 Tomcat 'lib')。除非您有非常具体的原因,否则不应在每次更新应用程序时重新部署驱动程序。
Placing JDBC driver to WEB-INF/lib is usually bad idea. Normally JDBC drivers are shared between all deployed applications and in proper configuration they should be part of global Tomcat configuration (put it to Tomcat 'lib'). The driver shouldn't be redeployed every time you update your application unless you have very specific reasons to this.