Dec 13, 2009

Tomcat and the mystery of the missing MySQL driver

If you have a Tomcat 5.x / MySQL app, upgrade the Tomcat version and it suddenly can't find the mysql driver - then this might apply to you.

If your project has a context.xml that explicitly names a datasource factory class like this:

<resource auth="Container" driverclassname="com.mysql.jdbc.Driver" factory="org.apache.commons.dbcp.BasicDataSourceFactory" name="jdbc/myDatabase" type="javax.sql.DataSource"/>
etc.. etc..

The "factory" attribute causes the problem on newer Tomcat's because they use their own factory class (org.apache.tomcat.dbcp.BasicDataSourceFactory) to deliver the datasource.
One solution that worked for me is:
  1. remove the "factory" attribute from your context.xml element.
  2. copy your mysql driver Jar into Tomcat's common/lib folder
  3. delete Tomcat's copy of your context.xml from Tomcat's conf\Catalina\localhost folder. It is recreated upon Tomcat start up and gets the same name as your app's .war file.

Netbeans and the frightfully forgetful class cache

While writing a unit tests in the brand flashing new NetBeans 6.8 it started complaining that the class under test was missing. No amount of code-completion magic nor import ingenuity would amend this. For all NetBeans could care - the class didn't exist. All that Netbeans would offer was to create a new class in the test source package... grrr.

Turns out that the Netbeans class cache can get a bit messed up after refactoring so the solution is simply to remove the cache folder and restart Netbeans.
  1. shut down Netbeans
  2. navigate to the cache index folder.
    • on linux it should live at:
      $HOME/.netbeans/6.8/var/cache/index
    • on Windows it's at:
      %USERPROFILE%\.netbeans\6.8\var\cache\index
  3. Remove the "index" folder
  4. Start NetBeans
This should apply to other versions of NetBeans also if you substitute the "6.8" in the paths with your version number.