I have a situation where I need to perform some work in a global tx.
For this reason, i have the following PersistenceUnit defined in my persistence.xml to get me a jta entityManager.
<persistence-unit name="resubEclipselink" transaction-type="JTA">
<jta-data-source>jdbc/XADataSource</jta-data-source>
...
</persistence-unit>
Now to persist I tried to proceed as follows #1:
if(isXA()){
mXAEntityManager.persist(entity);
mXAEntityManager.flush();
}
Things fail with an exception
javax.persistence.TransactionRequiredException:
Exception Description: No transaction is currently active
at
org.eclipse.persistence.internal.jpa.transaction.EntityTransactionWrapper.throwCheckTransactionFailedException(EntityTransactionWrapper.java:113)
I got the same reason when I begin a user transaction before proceeding.
So I tried another approach #2:
// get the transactional unit of work or null.
UnitOfWork uow = mEntityManager.getUnitOfWork();
uow.registerObject(entity);
uow.writeChanges();
uow.commit();
This sort of works but I am not sure if this is the right approach.
Will appreciate if someone can help me with why things don't work in the first case and if the second approach is fine?