0

I am upgrading entity beans in our application from EJB 2.0 to 3.0 version. I am using openjpa version 1.2.2 on weblogic server(10.3.0) and transaction type will be JTA.

I am facing the below error while committing the transaction post persisting the entity:

javax.transaction.SystemException: Illegal state (Expected: PrePrepared). BEA1-0A15322BC6A35D331713 at weblogic.transaction.internal.TransactionImpl.abortUnsync(TransactionImpl.java:1134) at weblogic.transaction.internal.ServerTransactionImpl.globalPrepare(ServerTransactionImpl.java:2172) at weblogic.transaction.internal.ServerTransactionImpl.internalCommit(ServerTransactionImpl.java:270) at weblogic.transaction.internal.ServerTransactionImpl.commit(ServerTransactionImpl.java:230) at weblogic.transaction.internal.TransactionManagerImpl.commit(TransactionManagerImpl.java:283) at weblogic.transaction.internal.TransactionManagerImpl.commit(TransactionManagerImpl.java:277) at com.bt.cp.entities.EntityBeansMultiThreadTest$Loader.call(EntityBeansMultiThreadTest.java:104) at com.bt.cp.entities.EntityBeansMultiThreadTest$Loader.call(EntityBeansMultiThreadTest.java:78) at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:885) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907) at java.lang.Thread.run(Thread.java:619)

Below is the code snippet which causes this error:

public Boolean call() throws Exception {
                        EntityManager entityManager = entityManagerSingleton.createEntityManager();
                        UserTransaction tx = entityManagerSingleton.createTransaction();

                        try {
                                tx.begin();
                                // Join the EntityManager operations to this UserTransaction
                                entityManager.joinTransaction();

                                entityManager.persist(new Party());

                                tx.commit();

                        } catch(Exception e) {
                                e.printStackTrace()
                        }

                        return true;
                }

Below is my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
    <persistence-unit name="testDataSource" transaction-type="JTA">
        <jta-data-source>testDataSource</jta-data-source>
        <class>test.Party</class>
        <class>
        .........................
                    </class>
        <properties>
            <property name="openjpa.QueryCache" value="true(CacheSize=1000)"/>
        </properties>
    </persistence-unit>
</persistence>

This error is not seen with openjpa version 1.1.0(part of weblogic 10.3.0). However, there is a major bug with version 1.1.0(https://issues.apache.org/jira/browse/OPENJPA-466) and hence, I am using version 1.2.2.

How can this issue be resolved? Anybody has any pointers on this. Any help is appreciated.

Niru
  • 80
  • 1
  • 6

1 Answers1

1

You state that there is a bug with OpenJPA. I would not make that claim based on what you have posted. As a matter of fact, the "SystemException: Illegal state" seems to be entirely WebLogic on the stack; I do not see OpenJPA on the stack. If there is more to the stack, and an OpenJPA exception occurs, please post it here. Otherwise, I think you'd be better off treating this as a WebLogic issue and specifically something with their Tx processing. Furthermore, if possible you should move to a newer OpenJPA e.g. 2.2.x or 2.4.x as 1.1.x and 1.2.x are very old (although WebLogic might be "hard wired" to a certain JPA/OpenJPA version so you'd need to check with them first). Next, I do wonder about potential threading issues here. I'm likely making too much of the names, but I see you have a "entityManagerSingleton.createEntityManager" and "EntityBeansMultiThreadTest", as well as the stack seems to start from a thread pool. I would suggest that you seriously take a look for any threading issue and make certain the EntityManager isn't used by multiple threads!! The JPA spec makes it clear that an EntityManager is not thread safe. An EntityManagerFactory is thread safe, but not an EntityManager. If two or more threads are using the same EntityManager, and since your code shows you are joining and committing the Tx, the threads could easily stomp on each other and cause a bad (Illegal) Tx state. Finally, since you are using application managed entity managers, you need to handle the life cycle of the EntityManager, meaning you need to close the EntityManager when you are done with it. In the above code, I do not see you calling 'close' on the 'entityManager' instance. By not doing this, you could be leaving the EntityManager in a state such that it can not be GC'd. Yes, the 'entityManager' instance is scoped to the method itself, however, the JPA provider could still retain data structures for the EntityManager since it doesn't know the EntityManager is no longer in use (i.e. since it was never told to 'close' it).

Thanks,

Heath

  • Thanks for your reply :) The bug I mentioned was with version 1.1.0 and hence I am using 1.2.2 which gives me the mentioned error(please see my edit). I also agree that this is something to do with weblogic/JTA transaction issue, however I am trying to figure what exactly causes this issue. I have also verified that this is not an issue with multithreading as the same error occurs in single thread environment. – Niru Oct 04 '16 at 04:29