Here is an example (well sort of).
Setup - you have a SLSB using CMT with a method on it that has 'Required' transaction semantics, see below doSomething().
Code:
public class TestBean implements javax.ejb.SessionBean {
private SessionContext ctx;
/// .. All the create etc routines.
public void setSessionContext(javax.ejb.SessionContext ctx) {
this.ctx = ctx;
}
// Business methods - with TX semantics already started so just load session and go
public String doSomething() {
String retVal;
Session sess;
try {
// ... Do you code here no Tx stuff necessary
sess = HibernateUtil.getSession();
sess.save(new X());
retVal = "Yes it worked";
}
catch (Exception ex) {
// Do something appropriate then rollback
ctx.setRollbackOnly();
retVal = "No it was rolled back";
}
HibernateUtil.closeSession(sess);
return retVal;
}
}
When the execution entered the doSomething method a CMT TX is started.
See how the SessionContext is used to tell (hint) that it CMT needs to rollback the CMT transaction. See
http://java.sun.com/j2ee/sdk_1.3/techdocs/api/javax/ejb/SessionContext.html
For more details on the SessionContext.