12

I looked everywhere on the web in order to find a solution to my problem, but I had no luck! :(

I'm trying to develop a servlet able to connect to a MySQL database (Connection Pool) and to deploy it on a Tomcat 8 server.

I have a context.xml file in META-INF like this:

<?xml version="1.0" encoding="UTF-8"?>
   <Context antiJARLocking="true" path="/DBConnectionPoolTest">
       <Resource name="jdbc/testdb"
                 auth="Container"
                 type="javax.sql.DataSource"
                 username="xxx" password="xxx"
                 driverclassname="com.mysql.jdbc.Driver"
                 url="jdbc:mysql://xxx/myApp"
                 maxactive="10"
                 maxidle="4" />
   </Context>

In WEB-INF I created the web.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">   
    <resource-ref>
        <description>DB Connection</description>
        <res-ref-name>jdbc/testdb</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
</web-app>

Finally, on the servlet class, I use:

Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("jdbc/testdb");
...
connection = dataSource.getConnection();

But on this line, when I try to get a connection from the datasource, I get the following exception:

java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'jdbc:mysql://xxx/myApp'
t org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2065)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:1939)
    at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1412)
    at DBPoolConnectionServlet.processRequest(DBPoolConnectionServlet.java:73)
...
Caused by: java.sql.SQLException: No suitable driver

I thought it could be caused by the absence of the JDBC Driver, so I putted mysql-connector-java-5.1.34-bin.jar in:

  1. pom.xml dependency
  2. Tomcat LIB folder
  3. WEB-INF/lib folder inside the webapp

but without luck.

Can you please tell me what I'm doing wrong? I feel so noob... :(

Thank you very much for your time spent reading (and hopefully answering) my question!!!

Brutus
  • 790
  • 3
  • 10
  • 27

3 Answers3

5

Looks like you are doing all fine, so:

driverClassName="com.mysql.jdbc.Driver" (the capitals may matter).

You place the mysql-connector-java-5.1.34-bin.jar (it has to have jar extension to be detected in tomcat/lib (dont put it on your webapp path it should be loaded by tomcat class loader).

If it is not helping and you are starting your webapp from IDE. Try to start tomcat form console and deploy your app manually. If you have more than one tomcat installed make sure that CATALINA_HOME is set to the one you place your mysql jar.

Zielu
  • 8,312
  • 4
  • 28
  • 41
  • I tried the manual deploy but the result is the same. In any case, going deeper through the stacktrace I noticed the following error: `Caused by: java.sql.SQLException: No suitable driver` – Brutus Feb 26 '15 at 08:47
  • Guy, thank you very much!!! You someway solved my issue!!! :D After looking around for the `No suitable driver` exception i found this question: http://stackoverflow.com/questions/5556664/how-to-fix-no-suitable-driver-found-for-jdbcmysql-localhost-dbname-error-w and in particular this answer: http://stackoverflow.com/a/19259608/1133284 For some reason I need to call `Class.forName("com.mysql.jdbc.Driver");`, even if I should not!!! :) – Brutus Feb 26 '15 at 09:23
  • Do you have to call Class.forName every time you need a connection? – RTF Nov 12 '15 at 10:11
  • @RTF No, I think you just need once, before any connection! – Brutus Mar 08 '16 at 08:30
2

Possible issue might be Tomcat encounters the driver jar multiple times during classloading. The driver jar only needs to be under the Tomcat lib directory and not under WEB-INF/lib of your web application. Having the driver jar at both these locations resulted in weird class loading issues for me which manifested in the form of errors suggesting "jdbc driver with empty class and null url could not be created". See this answer for a similar issue in a different database: https://stackoverflow.com/a/11604084/2200690

Update : I no longer start Tomcat from within Eclipse to make sure the Eclipse and Tomcat settings don't get mixed up (Instead I just start tomcat from the bin directory and remote debug the jvm from Eclipse).Once I made this change, I could have the driver jar in both the Web-INF/lib and Tomcat lib directory and it didn't matter.

Suketu Bhuta
  • 1,871
  • 1
  • 18
  • 26
2

Steps to fix Netbeans/Tomee/MySQL connection issues and resolve FAIL - Deployed application at context path /RA7Web-1.0-SNAPSHOT but context failed to start:

  1. Edit server.xml on your Tomee server using a full path name for appBase:

     <!-- Fixed the problem of cannot deploy by providing a full path  for appBase -->`
     <Host name="localhost"  
           appBase="C:\apache\apache-tomee-7.0.2-plume\webapps"
           unpackWARs="true" autoDeploy="true">
    
  2. Add the following to your project web-xml:

    <resource-ref>
        <description>Resource Allocation database</description>
        <res-ref-name>jdbc/resourcealloc</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
    </resource-ref>
    
  3. Add the following to your Context.xml:

      <Resource name="jdbc/resourcealloc"
            auth="Container"
            type="javax.sql.DataSource"
            username="root"
            password="mySecretPwd"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://localhost:3306/resourcealloc"
            maxActive="15"
            maxIdle="3"/>
    
  4. The following resources.xml was generated, but I had to edit several back-slashes out so it matched my URL:

      <resources>
          <Resource id="jdbc/resourcealloc" type="javax.sql.DataSource">
              jdbcDriver=com.mysql.jdbc.Driver
              password=mySecretPwd
              userName=root
              jdbcUrl=jdbc:mysql://localhost:3306/resourcealloc?zeroDateTimeBehavior=convertToNull
          </Resource>
      </resources>
    
Tanner Babcock
  • 3,232
  • 6
  • 21
  • 23
Jim Reitz
  • 41
  • 3