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)