transactions - javax.persistence.TransactionRequiredException after setRollbackOnly -
i have in containter-managed transaction- bean this:
@persistencecontext private entitymanager em; @resource private ejbcontext ejbcontext; public void testtransaction() { model model1 = new model(); em.persist(model1); ejbcontext.setrollbackonly(); model model2 = new model(); em.persist(model2);//the line problem }
in last line (with problem) transactionrequiredexception thrown:
javax.ejb.ejbexception: javax.persistence.transactionrequiredexception: jbas011469: transaction required perform operation (either use transaction or extended persistence context)
but in mastering ejb 4th edition book (search "doomed transactions" or go page 299) explained so, not thrown such exceptions, , instead should check ejbcontext.getrollbackonly()
before resource-hungry operations.
of course in simple example avoid problem throwing exception annotated @applicationexception(rollback=true)
, wonder miss.
by calling ejbcontext.setrollbackonly() have aborted current transaction started container. after there no transaction can associated em.persist(model2);. exception. check whether transaction active use getrollbackonly() method in ejbcontext return false.
Comments
Post a Comment