I use commons-dbcp to create a connection pool for the MySQL database in a small Spring web application that deploys as a WAR file to the local Tomcat 6.0.28 container (installed using the Ubuntu package manager) using the Sun 1.6 JDK. Deployment is done with Maven using tomcat-maven-plugin-1.1 with the goal of tomcat:redeploy . The MySQL / J jar connector is located in the webapp WEB-INF/lib .
When you first load webapp after starting Tomcat, everything works fine. However, when I redeploy webapp as Tomcat undeploys 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 cannot connect to the database, complaining about the lack of a 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 you manually restart Tomcat after redeployment.
I know that the MySQL driver is registered with the DriverManager when the class loads, but the com.mysql.jdbc.Driver class does not seem to restart when the webapp is redistributed. Does Tomcat WebappClassLoader rely on reloading classes from the WEB-INF/lib directory when reloading the webapp or storing them in memory between deployments? Or do I need to re-register the MySQL driver myself after deployment?
In addition, I saw a lot of messages suggesting moving the jar driver to the Tomcat commons/lib directory, but I would like to avoid dependencies that are external to my WAR file as much as possible.
Thanks in advance for any help.
matts source share