title: Creating an application managed JTA EntityManager
body: <p>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"</p>
  
  <pre><code>&lt;persistence-unit name="myPU" transaction-type="JTA"&gt;
  </code></pre>
  
  <p>In a bean I create the EntityManagerFactory as</p>
  
  <pre><code>EntityManagerFactory emf = Persistence.createEntityManagerFactory("myPU");
  </code></pre>
  
  <p>and then create an EnityManager as</p>
  
  <pre><code>EntityManager em = emf.createEntityManager();
  </code></pre>
  
  <p>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.</p>
  
  <p>I know we should have the entity manager and entity manager factory injected but i would like to understand this behavior</p>
  
  <p>The persistence.xml looks like this:</p>
  
  <pre><code>    &lt;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"&gt;
        &lt;persistence-unit name="myPU" transaction-type="JTA"&gt;
          &lt;provider&gt;org.eclipse.persistence.jpa.PersistenceProvider&lt;/provider&gt;
          &lt;jta-data-source&gt;java:app/jdbc/myDatasource&lt;/jta-data-source&gt;
          &lt;class&gt;example.MyEntity&lt;/class&gt;  
          &lt;properties&gt;
            &lt;property name="eclipselink.ddl-generation.output-mode" value="sql-script"/&gt;
            &lt;property name="eclipselink.application-location" value="C:\gen-ddl"/&gt;      
          &lt;/properties&gt;
        &lt;/persistence-unit&gt;
      &lt;/persistence&gt;
  </code></pre>
  
  <p>The glassfish-resource.xml file in my EAR project where I have defined the datasource looks like this:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
      &lt;!DOCTYPE resources PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Resource Definitions//EN" 
      "http://glassfish.org/dtds/glassfish-resources_1_5.dtd"&gt;
      &lt;resources&gt;
          &lt;jdbc-connection-pool  name="MyDB_Pool" datasource-classname="oracle.jdbc.pool.OracleDataSource" res-type="javax.sql.DataSource"&gt;
              &lt;property name="url" value="jdbc:oracle:thin:@192.168.xxx.xxx:1521:xxx"/&gt;
              &lt;property name="user" value="xxx"/&gt;
              &lt;property name="password" value="xxx"/&gt;
          &lt;/jdbc-connection-pool&gt;
  
          &lt;jdbc-resource 
              enabled="true" 
              jndi-name="java:app/jdbc/myDatasource" 
              object-type="user" 
              pool-name="MyDB_Pool"/&gt;
      &lt;/resources&gt; 
  </code></pre>