I'm trying to instantiate an EntityManager using a JTA Datasource, but I always get NullPointerException. Here's my environment: Server : JBossAS 7.0 JPA : 2.0 DB : MySQL
persistence.xml
<?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="WebStock" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>stockmanagementDS</jta-data-source>
<class>prv.stockmanagement.business.Product</class>
</persistence-unit>
</persistence>
On a dao class I'm trying this :
@PersistenceContext(type=PersistenceContextType.EXTENDED, unitName="WebStock")
private EntityManager em;
but when I use its getter, I get NullPointerException here :
public EntityManager getEntityManager() {
return em;
}
the debug shows it's null.
Here's the datasource definition in the standalone.xml :
<datasource jndi-name="stockmanagementDS" pool-name="stockmanagement" enabled="true" jta="true" use-java-context="true" use-ccm="true">
<connection-url>
jdbc:mysql://localhost:3306/kitchen_stock
</connection-url>
<driver>
mysql
</driver>
<security>
<user-name>
root
</user-name>
<password>
rootroot
</password>
</security>
<statement>
<prepared-statement-cache-size>
100
</prepared-statement-cache-size>
<share-prepared-statements/>
</statement>
</datasource>
"mysql" is the name of the driver I defined after the datasource :
<drivers>
<driver name="mysql" module="com.sql.mysql">
<driver-class>
com.mysql.jdbc.Driver
</driver-class>
</driver>
<driver name="h2" module="com.h2database.h2">
<xa-datasource-class>
org.h2.jdbcx.JdbcDataSource
</xa-datasource-class>
</driver>
</drivers>
Persistence is under META-INF, it's dynamic web project, and it's working well. The classing using the entity manager is not an EJB.
Any hint?