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,
[ February 26, 2008: Message edited by: Beno�t de CHATEAUVIEUX ]
Thanks for your answers.
Gladwin > Now I know that a commit on a transaction marked as RollbackOnly will throw a javax.transaction.RollbackException
Narendra > UserTransaction has a setRollbackOnly() method too.
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.
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 22.214.171.124 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,
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.
Originally posted by Narendra Dhande:
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 ]