mkayman
mkayman

Reputation: 110

No suitable driver found for jdbc:mysql://localhost:3306/test

When i try to run my JSF application on Tomcat 7, it throws this exception.

Caused by: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/test
at java.sql.DriverManager.getConnection(Unknown Source)
at java.sql.DriverManager.getConnection(Unknown Source)
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192)
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:278)
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297)
... 51 more

If I add this line before creating EntitiyManagerFactory, it works fine.

Class.forName("com.mysql.jdbc.Driver");
emf = Persistence.createEntityManagerFactory("manager1");

My dependencies are

<dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.1.2.Final</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.19</version>
    </dependency>

Also my application works fine tomcat 6, without adding Class.forName("com.mysql.jdbc.Driver");

Any idea to solve this problem? Thanks

Upvotes: 0

Views: 11002

Answers (5)

duckegg
duckegg

Reputation: 1389

I have similar issue. It is solved by upgrading hibernate to 4.1.4final.

Upvotes: 1

JBirch
JBirch

Reputation: 659

First and real solution: Try with Hibernate 4.1.0 and 4.1.3.Final.

I ran into this issue on Hibernate 4.1.2 before eventually stumbling across https://forum.hibernate.org/viewtopic.php?p=2454336. As far as I can see, it's a bit of an incompatibility between the later versions of Tomcat (In its fixes for the leaky nature of the DeviceManager) and Hibernate 4.1.2.

The other workarounds are to invoke the registration of the driver explicitly, as in Brad Whitaker's answer, or to make sure the JreMemoryLeakPreventionListener isn't protecting the DriverManager by adding driverManagerProtection="false" to the Listener definition in $CATALINA_HOME/conf/server.xml - that is:

<Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" driverManagerProtection="false">

Upvotes: 6

Brad Whitaker
Brad Whitaker

Reputation: 1174

I encountered this same problem when I migrated a working .war from Tomcat 5.5 and Java 6 to Tomcat 7 and Java 7. My previously working .war started throwing "java.sql.SQLException: No suitable driver found for jdbc:sqlserver...". I was able to resolve by simply adding

Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

immediately before invoking

java.sql.DriverManager.getConnection("myUrl");

I'm aware that documentation says this additional call is not required with the jdbc4 driver, but this fixed my problem. The driver jar (sqljdbc4.jar) was located in WEB-INF/lib of my .war.

Upvotes: 4

djmj
djmj

Reputation: 5554

On some servers (jboss, glassfish) I also had to put the driver jar into the server default library folder.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240966

The problem is driver class isn't there available at runtime for your application, put the driver jar into /WEB-INF/lib

Upvotes: 1

Related Questions