0
votes

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> 
1
Are you trying to access the EMF in the web tier or EJB tier?NBW
In the EJB tier. I tried something different. I have 2 EJB modules (2 separate jars in the same EAR). I got the EMF injected in one EJB module and then passed it to the other EJB module. The call emf.createEntityManager() results in an IllegalStateException with the message as - cannot find EntityManagerFactory for persistence unit myPU. Is this expected?Arc
The difference between getting EntityManagerFactory by Persistence.createEntityManagerFactory("myPU") and getting it injected is that i get EntityManagerImpl instance in the 1st case while EntityManagerWrapper in the later.Arc

1 Answers

1
votes

Check and include your persistence.xml. Are you using a JTA DataSource or specifying a JDBC connection?

If you want to use JTA, then you must use a JTA DataSource, otherwise you need to use RESOURCE_LOCAL.

If you create an EntityManager after the JTA transaction has started, it should automatically join the transaction. Otherwise, you need to call joinTransaction().