Note that there are some explanatory texts on larger screens.

plurals
  1. POCannot create and close multiple EntityManagers in a SessionBean method
    primarykey
    data
    text
    <p>I'm trying to use OpenJPA Slice as a persistence mechanism for one of my classes inside a JBoss 4.0.5 (EJB 2.1) application server and I'm experiencing some problems.</p> <p>In a J2SE setting the following code (create an EntityManager, use it, close it, create a new one, use it close it) works fine.</p> <pre><code>EntityManagerFactory factory = Persistence.createEntityManagerFactory("my_persistence_unit"); EntityManager em = factory.createEntityManager(); Query q = em.createQuery("select m from mytable m"); List result = q.getResultList(); // ... do something with result em.close(); em = factory.createEntityManager(); q = em.createQuery("select m from myothertable m"); result = q.getResultList(); // ... do something with result em.close(); </code></pre> <p>But if I try to do the same thing in a StatelessSessionBean (again: EJB 2.1)</p> <pre><code>// the entity manager FACTORY is a static variable defined elsewhere EntityManager em = FACTORY.createEntityManager(); Query q = em.createQuery("select m from mytable m"); List result = q.getResultList(); // ... do something with result em.close(); em = FACTORY.createEntityManager(); q = em.createQuery("select m from myothertable m"); result = q.getResultList(); // ... do something with result em.close(); </code></pre> <p>... I get an exception on the second query execution:</p> <pre><code>&lt;openjpa-1.0.1-r420667:592145 fatal user error&gt; org.apache.openjpa.persistence.InvalidStateException: The context has been closed. </code></pre> <p>The same thing happens if I have only one query in my SessionBean method and I remote call it twice. The first call works just fine but the second call would throw the same exception.</p> <p>It seems like all queries in the SessionBean are executed against the context of the first EntityManager ever created in the session bean. If I don't call close() on the first EntityManager, everything works fine.</p> <p>I'm puzzled why there's any interference between SessionBeans and JPA EntityManagers at all since JBoss 4 is so old that it shouldn't know anything about JPA.</p> <p>This is the persistence.xml for the J2SE setup: com.mycompany.MyEntity</p> <pre><code> &lt;properties&gt; &lt;property name="openjpa.BrokerFactory" value="slice"/&gt; &lt;property name="openjpa.slice.Names" value="One,Two"/&gt; &lt;property name="openjpa.slice.Master" value="One"/&gt; &lt;property name="openjpa.slice.Lenient" value="true"/&gt; &lt;property name="openjpa.slice.One.ConnectionURL" value="jdbc:mysql://localhost/jpa_test_1"/&gt; &lt;property name="openjpa.slice.Two.ConnectionURL" value="jdbc:mysql://localhost/jpa_test_2"/&gt; &lt;property name="openjpa.slice.DistributionPolicy" value="com.mycompany.RandomDistributionPolicy"/&gt; &lt;property name="openjpa.ConnectionDriverName" value="com.mysql.jdbc.Driver"/&gt; &lt;property name="openjpa.ConnectionUserName" value="user"/&gt; &lt;property name="openjpa.ConnectionPassword" value="password"/&gt; &lt;property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/&gt; &lt;!-- --&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; </code></pre> <p>This is the persistence.xml for the JBoss setup:</p> <pre><code>&lt;persistence-unit name="my_persistence_unit" transaction-type="RESOURCE_LOCAL"&gt; &lt;class&gt;com.mycompany.MyEntity&lt;/class&gt; &lt;properties&gt; &lt;property name="openjpa.BrokerFactory" value="slice"/&gt; &lt;property name="openjpa.slice.Names" value="One"/&gt; &lt;property name="openjpa.slice.Master" value="One"/&gt; &lt;property name="openjpa.slice.Lenient" value="true"/&gt; &lt;property name="openjpa.ConnectionUserName" value="user"/&gt; &lt;property name="openjpa.ConnectionPassword" value="password"/&gt; &lt;property name="openjpa.slice.One.ConnectionURL" value="jdbc:mysql://localhost/jpa_test_1"/&gt; &lt;property name="openjpa.slice.One.ConnectionDriverName" value="com.mysql.jdbc.Driver"/&gt; &lt;property name="openjpa.slice.Two.ConnectionURL" value="jdbc:mysql://localhost/jpa_test_2"/&gt; &lt;property name="openjpa.slice.Two.ConnectionDriverName" value="com.mysql.jdbc.Driver"/&gt; &lt;property name="openjpa.slice.ThreadingPolicy" value="cached"/&gt; &lt;property name="openjpa.slice.DistributionPolicy" value="com.mycompany.RandomDistributionPolicy"/&gt; &lt;property name="openjpa.jdbc.DBDictionary" value="mysql"/&gt; &lt;property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema"/&gt; &lt;property name="openjpa.Log" value="DefaultLevel=INFO, Runtime=TRACE, Tool=INFO, SQL=TRACE"/&gt; &lt;property name="openjpa.jdbc.QuerySQLCache" value="false"/&gt; &lt;/properties&gt; &lt;/persistence-unit&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload