1

Common question, I know, but I can't figure it out. Tomcat 6.0.41 with a very standard setup, single host. When I try to get a JDBC database connection from a DataSource, I get the following error:

org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class '' for connect URL 'null'
... Caused by: java.sql.SQLException: No suitable driver

My JDBC driver jar file (MySQL Connector/J) is located at /usr/share/tomcat6/lib/mysql-connector-java-5.1.35-bin.jar.

Here's my context.xml file:

<context>

  <Resource name="jdbc/first_db" 
        auth="Container" 
        type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        username="..." 
        password="..." 
        url="jdbc:mysql://my.host.com:3306/first_db"
        connectionProperties="verifyServerCertificate=false;useSSL=true;requireSSL=true"/>

  <Resource name="jdbc/second_db" 
        auth="Container" 
        type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver"
        maxActive="100" 
        maxIdle="30" 
        maxWait="10000"
        username="..." 
        password="..."
        url="jdbc:mysql://my.host.com:3306/second_db"
        connectionProperties="verifyServerCertificate=false;useSSL=true;requireSSL=true"/>

</context> 

I have this in my web.xml:

<resource-ref>
    <description>First DB Connection</description>
    <res-ref-name>jdbc/first_db</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

<resource-ref>
    <description>Second DB Connection</description>
    <res-ref-name>jdbc/second_db</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

In my code, I'm doing this:

Context ctx = new InitialContext();
dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/first_db");
Connection conn = dataSource.getConnection(); // error here

I've tried putting the context.xml in the following locations (my webapp root is located at /var/www):

  • /etc/tomcat6/Catalina/locahost/context.xml
  • /etc/tomcat6/Catalina/context.xml
  • /var/www/WEB-INF/context.xml
  • /var/www/WEB-INF/META-INF/context.xml
  • /var/www/META-INF/context.xml

I've also tried adding my resource elements from the context.xml shown above to the global context config at /etc/tomcat6/context.xml. Everything I've tried results in the same JDBC error.

I just changed the JNDI lookup string in my code to something that doesn't exist, and I got this:

javax.naming.NameNotFoundException: Name doesNotExist is not bound in this Context

...so the fact that I don't normally see this error must mean that the context.xml file is being found and processed. So that means that Tomcat just can't find the MySQL driver. But it is definitely in /usr/share/tomcat6/lib. I updated the driver to the latest version just now (v5.1.35) but it still gives the same error.

I've also just removed the mysql driver jar file from my WEB-INF/lib directory, because some people are saying there might be an issue having it there and in the tomcat system lib directory. Still the problem though.

RTF
  • 6,214
  • 12
  • 64
  • 132

1 Answers1

0

Do you have the mysql JDBC JAR in the Tomcat lib folder?

See this related answer. It could be that Tomcat can't load the driver.

Community
  • 1
  • 1
tom
  • 1,331
  • 1
  • 15
  • 28
  • I do have the JDBC drivers in `/usr/share/tomcat6/lib` – RTF Jun 27 '15 at 11:45
  • Are you using Maven to ensure all dependencies are included in your WAR? – tom Jun 27 '15 at 11:49
  • I'm not using Maven, but right now the file `/usr/share/tomcat6/lib/mysql-connector-java-5.1.14-bin.jar` exists and so does the file `/var/www/WEB-INF/lib/mysql-connector-java-5.1.14-bin.jar` (that's where my webapp dependencies are located). – RTF Jun 27 '15 at 11:55
  • I would check out [this answer](http://stackoverflow.com/questions/11516747/cannot-create-jdbc-driver-of-class-for-connect-url-null-i-do-not-underst). It looks like the same problem. – tom Jun 27 '15 at 12:00