This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I know that, in case of CMT, a transaction marked as rollBackOnly will rollback instead of commit.
I the case of BMT, I can't find the expected behaviour in the specs. For me, a code like this
will throws an exception because you can't do a commit in a Transaction marked for rollback. I read in the Javadoc of javax/transaction/UserTransaction.html#commit(): RollbackException - Thrown to indicate that the transaction has been rolled back rather than committed.
I tested it in my JBoss and I get: Exception in thread "main" javax.transaction.RollbackException: [com.arjuna.ats.internal.jta.transaction.arjunacore.commitwhenaborted] [com.arjuna.ats.internal.jta.transaction.arjunacore.commitwhenaborted] Can't commit because the transaction is in aborted state
Thanks in advance for your opinion,
Beno�t [ February 26, 2008: Message edited by: Beno�t de CHATEAUVIEUX ]
Though the UserTransaction has the setRollbackOnly method, In BMT your transaction is demarcated by your ut.begin and ut.commit or ut.rollback commands. EJB spec enforce that you can not call setRollbackOnly for BMT transaction, otherwise it will throw the Exception which you are receiving.
When you are using the BMT tracsaction, you are controlling the transaction, there is no need to notify the container that the transaction is for the rollback only as the container is not controlling your transaction demarcation.
Benoît de Chateauvieux
Joined: Aug 10, 2007
I have tested it with this code:
And it doesn't throw any exception.
You can call setRollbackOnly for BMT transaction but, of course, you can't do a commit on a Transaction marked for rollback.
The chapter 184.108.40.206 of the spec is about EJBContext.getRollbackOnly():
An enterprise bean with bean-managed transaction demarcation must not use the getRollbackOnly and setRollbackOnly methods of the EJBContext interface.
An enterprise bean with bean-managed transaction demarcation has no need to use these methods, because of the following reasons:
� An enterprise bean with bean-managed transaction demarcation can obtain the status of a transaction by using the getStatus method of the javax.transaction.UserTransaction interface.
� An enterprise bean with bean-managed transaction demarcation can rollback a transaction using the rollback method of the javax.transaction.UserTransaction interface.
Thanks for your help,
Joined: Dec 04, 2004
Sorry for my previous post. I had not checked if the container throws exception or not when we used setRollbackOnly only for user managed transactions. While reading the Spec I understand only that this method is not very useful when the transaction is not CMT. If you determine that the transaction have no fruitful results, you can rollbak the transaction at same point only, as you have full control on the transaction, there is no need to set the transaction for rollbak only and then rollback the transaction.
I believe that in the case of BMT, only BMT can invoke UserTransaction methods in the user code. EJBContext methods setRollbackOnly() and getRollbackOnly() only apply to CMT. UserTransaction methods include commit(), rollback(), getStatus(), and setRollbackOnly().
Originally posted by Narendra Dhande: Hi,
The setRollbakOnly() is applicable to the CMT only. You can not use it with User transaction. To rollback the user transaction yo have to explicitly use the rollback() instead of commit().
[ March 11, 2008: Message edited by: Frank Stein ]