Note that there are some explanatory texts on larger screens.

plurals
  1. POTomcat webapp cannot use MySQL driver after redeploy
    text
    copied!<p>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 <code>tomcat:redeploy</code>. The MySQL Connector/J jar is in the webapp's <code>WEB-INF/lib</code> directory.</p> <p>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.</p> <pre>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 </pre> <p>Then, when Tomcat restarts the webapp, it fails to connect to the database, complaining about no suitable driver:</p> <pre><code>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 </code></pre> <p>Everything works fine if I manually restart Tomcat after the redeploy.</p> <p>I know the MySQL driver registers itself with the DriverManager when the class is loaded, but the <code>com.mysql.jdbc.Driver</code> class doesn't seem to be reloaded when the webapp is redeployed. Is Tomcat's WebappClassLoader supposed to reload classes from the <code>WEB-INF/lib</code> 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?</p> <p>Also, I've seen plenty of posts suggesting moving the driver jar to Tomcat's <code>commons/lib</code> directory, but I would like to avoid dependencies external to my WAR file as much as possible. </p> <p>Thank you in advance for any assistance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload