Simple, EJBContext != UserTransaction.
From the J2EE API:
public void setRollbackOnly()
Mark the current transaction for rollback. The transaction will become permanently marked for rollback. A transaction marked for rollback can never commit. Only enterprise beans with container-managed transactions are allowed to use this method.
IllegalStateException - The Container throws the exception if the instance is not allowed to use this method (i.e. the instance is of a bean with bean-managed transactions).
I'm a little confused - aren't we getting the user transaction from the SessionContext in the SessionBean? Are you saying that:
is different from
UserTransaction ut = sessionContext.getUserTransaction(); ut.setRollbackOnly();?
If so - in what way?
Thank you very much.
well, a UserTransaction is a different object than the sessioncontext.
You can use a UserTransaction in an application outside of your EJBs (e.g. a web application) to execute several remote calls to your EJBs within one transaction context.
My last post is not quite complete.
UserTransactions are of course also use in Beans with BMT. However, the UserTransaction object is the object that you have to use to start, commit and rollback your transactions. The EJBContext holds the state for an EJB (CMT and BMT) and is managed by the AppServer, which is why you should not call setRollback on the context.