This content has been marked as final.
Show 1 reply
-
1. Re: EJB 3 + transaction propagation
charlf Dec 11, 2008 5:32 AM (in response to david81)Hey all,
I also got this error when using the TransactionManager, which is about the same as the UserTransaction.
The problem was that I was using the the TM while it was still busy with another transaction, you have to check the status of the TM first. heres how I fixed it.EntityManager EM = EMF.createEntityManager(); TransactionManager TM = (TransactionManager)ctx.lookup("java:TransactionManager"); boolean suspended = false; Transaction t = null; if(TM.getStatus() == Status.STATUS_NO_TRANSACTION || TM.getStatus() == Status.STATUS_UNKNOWN) { TM.begin(); EM.joinTransaction(); } if(TM.getStatus() == Status.STATUS_ACTIVE) { suspended = true; t = TM.suspend(); TM.begin(); EM.joinTransaction(); } else { throw new Exception("Transaction status invalid: "+TM.getStatus()); } //Do Actual Work with DB EM.persist(Entity); EM.flush(); TM.commit(); if(suspended == true) { TM.resume(t); } TM = null; EM = null;