I am using Glassfish 3.1.2, JPA2.0, eclipselink. I am trying to create an application managed EntityManager. The transaction type for the persistence unit in the persistence.xml file is specified as "JTA"
<persistence-unit name="myPU" transaction-type="JTA">
In a bean I create the EntityManagerFactory as
EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPU");
and then create an EnityManager as
EntityManager em = emf.createEntityManager();
The question is: would the entity manager that I get this way be JTA? I tried this and I am able to call the getTransaction() method on the entity manager without an exception which to my understanding should not be allowed for a JTA entity manager. Also if I use this entity manager in a bean managed transaction (with the entity manager being created after the transaction was begun) nothing gets persisted in the DB after the commit on the user transaction.
I know we should have the entity manager and entity manager factory injected but i would like to understand this behavior
The persistence.xml looks like this:
<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="myPU" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:app/jdbc/myDatasource</jta-data-source>
<class>example.MyEntity</class>
<properties>
<property name="eclipselink.ddl-generation.output-mode" value="sql-script"/>
<property name="eclipselink.application-location" value="C:\gen-ddl"/>
</properties>
</persistence-unit>
</persistence>
The glassfish-resource.xml file in my EAR project where I have defined the datasource looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN"
"http://glassfish.org/dtds/glassfish-resources_1_5.dtd">
<resources>
<jdbc-connection-pool name="MyDB_Pool" datasource-classname="oracle.jdbc.pool.OracleDataSource" res-type="javax.sql.DataSource">
<property name="url" value="jdbc:oracle:thin:@192.168.xxx.xxx:1521:xxx"/>
<property name="user" value="xxx"/>
<property name="password" value="xxx"/>
</jdbc-connection-pool>
<jdbc-resource
enabled="true"
jndi-name="java:app/jdbc/myDatasource"
object-type="user"
pool-name="MyDB_Pool"/>
</resources>