File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB Certification (SCBCD/OCPJBCD) and the fly likes SessionContext . setRollbackOnly() method doubt. 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 "SessionContext . setRollbackOnly() method doubt." Watch "SessionContext . setRollbackOnly() method doubt." New topic

SessionContext . setRollbackOnly() method doubt.

Deepak Jain
Ranch Hand

Joined: Aug 05, 2006
Posts: 637
Is this statement correct:
All session beans can call SessionContext.setRollbackOnly().

I tested with Stateless and Stateful and i was able to invoke without any issues.
ETS says: Only Enterprise beans with CMT can invoke the above method.

a) Is this statement correct, if yes please elaborate?
b) What are enterprise beans? I thought they were Session beans.

LakshmiNarayana Kodali

Joined: Oct 30, 2008
Posts: 16
setRollBackOnly() is available on SessionContext and MessageDrivenContext.

So this method works successfully if the following conditions are met

1. Session Beans or Message Driven Beans should use CMT (Transaction should be managed by container) and also
2. The method should be called in transactional context

It will throw IllegalStateException if
1. called in non transactional context or
2. called by the BMT bean

LakshmiNarayana Kodali
SCJP 5(98%), SCWCD(100%), SCBCD in progress
[ December 26, 2008: Message edited by: LakshmiNarayana Kodali ]
Chandrashekhar Raiphale

Joined: Aug 26, 2006
Posts: 14
Following is text form core specification. I hope this will clear your doubt.

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.User-Transaction interface.
� An enterprise bean with bean-managed transaction demarcation can rollback a transaction using the rollback method of the javax.transaction.UserTransaction interface.

The container must throw the java.lang.IllegalStateException if an instance of a bean with bean-managed transaction demarcation attempts to invoke the setRollbackOnly or getRollbackOnly method of the javax.ejb.EJBContext interface.

An enterprise bean with container-managed transaction demarcation can use the setRollbackOnly method of its EJBContext object to mark the transaction such that the transaction can never commit. Typically, an enterprise bean marks a transaction for rollback to protect data integrity before throwing an application exception, if the application exception class has not been specified to automatically cause the container to rollback the transaction.

For example, an AccountTransfer bean which debits one account and credits another account could mark a transaction for rollback if it successfully performs the debit operation, but encounters a failure during the credit operation.

Cheers,<br />Chandrashekhar <br />SCJP
I agree. Here's the link:
subject: SessionContext . setRollbackOnly() method doubt.
It's not a secret anymore!