jQuery in Action, 2nd edition*
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes UserTransaction and setRollbackOnly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » EJB Certification (SCBCD/OCPJBCD)
Bookmark "UserTransaction and setRollbackOnly" Watch "UserTransaction and setRollbackOnly" New topic
Author

UserTransaction and setRollbackOnly

Benoît de Chateauvieux
Ranch Hand

Joined: Aug 10, 2007
Posts: 183
Hi All,

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 ]

SCJP5 | SCBCD5 | SCEA5 Part 1
Gladwin Burboz
Greenhorn

Joined: Feb 26, 2008
Posts: 25
I don't understand what behavior you expect here?

You have set it to roll back. So whatever may be the condition, it has to rollback. Now if you attempt to commit after that, what you think should happen?


<a href="http://www.sealordresortarnala.com" target="_blank" rel="nofollow">http://www.sealordresortarnala.com</a>
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
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().

Thanks


Narendra Dhande
SCJP 1.4,SCWCD 1.4, SCBCD 5.0, SCDJWS 5.0, SCEA 5.0
Benoît de Chateauvieux
Ranch Hand

Joined: Aug 10, 2007
Posts: 183
Hi Gladwin and Narendra,

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.
See http://java.sun.com/j2ee/1.4/docs/api/javax/transaction/UserTransaction.html

Thanks,

Beno�t
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

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.

Thanks
Benoît de Chateauvieux
Ranch Hand

Joined: Aug 10, 2007
Posts: 183
Hi Narendra,

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 13.3.3.1 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,

Beno�t
Narendra Dhande
Ranch Hand

Joined: Dec 04, 2004
Posts: 950
Hi,

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.

Thanks
Frank Stein
Greenhorn

Joined: Feb 29, 2008
Posts: 2
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().

Thanks

[ March 11, 2008: Message edited by: Frank Stein ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: UserTransaction and setRollbackOnly