robkh
robkh

Reputation: 11

Advice needed to understand cause of java.sql.SQLException: No suitable driver found for jdbc:postgresql error

I am writing a web application and I am trying to use hibernate to connect to a PostgreSQL 8.4.13 database in my project. I have been unsuccessful so far, I get the following error (java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/mcappdb) – the trace is at the bottom. I have been through many forums and not found a solution to my problem:

I have included the following dependencies into my pom.xml and this builds successfully. I am deploying to jboss eap 6 using ‘mvn jboss-as:deploy’.

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>4.2.5.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>4.2.5.Final</version>
    </dependency>  
    <dependency>
        <groupId>postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>8.4-702.jdbc4</version>
    </dependency>

I have configured the persistence.xml file as follows:

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
         http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

<persistence-unit name="mcappdb" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <class>uk.co.metasphere.entities.userConfig</class>

    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
        <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/mcappdb"/>
        <property name="hibernate.connection.username" value="mcapp"/>
        <property name="hibernate.connection.password" value="mc2000"/>
        <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/>
    </properties>
</persistence-unit>

This file is in the src/main/resources/META-INF directory.

The app deploys successfully but raises the error when it tries to connect to the database with hibernate. My code to connect from my app is as follows:

EntityManagerFactory emf = Persistence.createEntityManagerFactory("mcappdb");
EntityManager em1 = emf.createEntityManager();
userConfig uscon = em1.find(userConfig.class, 0);

The error is raised on the call to em1.find().

I have read through many forum posts. Most indicate the error is either because the driver is not on the classpath or the defined url is wrong. The driver jar file is in /target/MCapp-1.0/WEB-INF/lib/postgresql-8.4-702.jdbc4.jar and is I believe deployed in the same place as all the other dependant jar files (correct?). The url I use works if I connect via a basic JBDC connection (rather than hibernate) so I believe that is also ok.

Another suggestion is that I need to load the driver using the following code:

Class.forName("org.postgresql.Driver");

This doesn’t help either.

Since the straight forward JDBC connection works ok it seems that Hibernate is the problem? Can anyone advise me of what the problem might be or how I could investigate it further? Where/how does hibernate look for the database driver? Do I need to deploy it to another directory or make another configuration step so that it can find it?

Any help gratefully received.

Trace of error:

15:12:53,932 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/MCapp-1.0].[Mcapp Application]] (http-/0.0.0.0:8080-1) Servlet.service() for servlet Mcapp Application threw exception: java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/mcappdb
at java.sql.DriverManager.getConnection(DriverManager.java:596) [rt.jar:1.7.0_25]
at java.sql.DriverManager.getConnection(DriverManager.java:187) [rt.jar:1.7.0_25]
at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:193) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:278) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:297) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.extractPhysicalConnection(ConnectionProxyHandler.java:82) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.proxy.ConnectionProxyHandler.continueInvocation(ConnectionProxyHandler.java:138) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at com.sun.proxy.$Proxy17.prepareStatement(Unknown Source)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:147) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:166) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:145) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1711) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.Loader.doQuery(Loader.java:828) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:289) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:259) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.Loader.loadEntity(Loader.java:2033) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:82) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:72) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:3719) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:449) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:418) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:204) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:251) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:148) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1079) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.internal.SessionImpl.access$2200(SessionImpl.java:172) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2425) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:975) [hibernate-core-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:802) [hibernate-entitymanager-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:776) [hibernate-entitymanager-4.1.3.Final-redhat-1.jar:4.1.3.Final-redhat-1]
at uk.co.metasphere.McappUI.init(McappUI.java:210) [classes:]
at com.vaadin.ui.UI.doInit(UI.java:607) [vaadin-server-7.1.5.jar:7.1.5]
at com.vaadin.server.communication.UIInitHandler.getBrowserDetailsUI(UIInitHandler.java:223) [vaadin-server-7.1.5.jar:7.1.5]
at com.vaadin.server.communication.UIInitHandler.synchronizedHandleRequest(UIInitHandler.java:73) [vaadin-server-7.1.5.jar:7.1.5]
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:37) [vaadin-server-7.1.5.jar:7.1.5]
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1371) [vaadin-server-7.1.5.jar:7.1.5]
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:238) [vaadin-server-7.1.5.jar:7.1.5]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.1.Final-redhat-1.jar:1.0.1.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:489) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.2.Final-redhat-1.jar:7.1.2.Final-redhat-1]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.2.Final-redhat-1.jar:7.1.2.Final-redhat-1]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:679) [jbossweb-7.0.16.Final-redhat-1.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:931) [jbossweb-7.0.16.Final-redhat-1.jar:]
at java.lang.Thread.run(Thread.java:724) [rt.jar:1.7.0_25]

Upvotes: 1

Views: 1741

Answers (1)

Andres Olarte
Andres Olarte

Reputation: 4380

Place your driver in the lib folder of your app server. The JPA libraries you're using are inside your appserver (not you application). Your appserver can't access classes in your application. See here

Upvotes: 1

Related Questions