My webapp is failing to get a connection to MySQL, failing with the error:
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
Now the mysql connector jar is clearly in my classpa开发者_StackOverflowth: mysql-connector-java-5.0.7-bin.jar, in the WEB-INF/lib directory.
So it doesn't make sense that it's "not found." I'm guessing it must be failing some static initialization.
Is there any way to diagnose this problem?
While I didn't come up with any good diagnostic utilities, aside from the stack trace, I did realize what was going on. Naturally the stacktrace has the clue:
Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1136)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:880)
The DataSource was defined in a context.xml file within the web application, which means that the driver was being loaded by Tomcat using the parent classloader to the web app. That classloader doesn't have access to the application's classpath.
The solution is to drop the mysql jar in Tomcat's shared library directory.
A different solution is to embed the DataSource within the app instead of using Tomcat's DataSource management, thus allowing a stock Tomcat distribution.
精彩评论