SQLException: No suitable driver found for jdbc:po

2019-07-29 11:10发布

问题:

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/database2

thrown by entityManager.getTransaction()

JBoss 7.1.1

Class.forName("org.postgresql.Driver");
EntityManager entityManager = Persistence.createEntityManagerFactory("database2").createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence 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"
    version="2.0">

    <persistence-unit name="database2" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <properties>
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.user" value="user" />
            <property name="javax.persistence.jdbc.password" value="" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://localhost:5432/database2" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />
        </properties>
    </persistence-unit>
</persistence>

Directory structure

EJB:
    |
    |- src
            |
            |- class1.java
            |- interface1.java
            |
            |- META_INF
                        |
                        |-persistence.xml

WEB:
    |
    |- src
    |       |
    |       |- class2.java
    |
    |-WebContent
                |
                |- WEB-INF
                            |
                            |- lib
                                    |
                                    |- postgresql-9.1-903.jdbc4.jar

EAP_HOME/modules/org/postgresql/main/module.xml

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.0" name="org.postgresql">
  <resources>
    <resource-root path="postgresql-9.1-903.jdbc4.jar"/>
  </resources>
  <dependencies>
    <module name="javax.api"/>
    <module name="javax.transaction.api"/>
  </dependencies>
</module>

CLI command:

/subsystem=datasources/jdbc-driver=postgresql:add(driver-name=postgresql,driver-module-name=org.postgresql,driver-xa-datasource-class-name=org.postgresql.Driver)

回答1:

As Nicolas says install the JDBC driver as a core module

Next you need to make you application depend on this module there are two ways: MANIEST.MF jboss-dependency.xml

I recommend the first way as it is more spec compliant. Essentially you need to create a MANIFEST.MF file in the META-INF directory of your EAR or WAR (whatever is being deployed). The line you create in the file will be something like:

Dependencies: org.postgresql

For further info see the JBoss EAP documentation or the JBoss wiki. I hope this helps!