Hi,
I have a servlet, a session bean, and an entity bean. A servlet doesn't have any transaction attribute, a session bean has Required, and an entity bean has Required. A session bean is called in remote, and an entity bean is called in local. I have the code like the following:
Servlet:
EJBDelegate ed = new EJBDelegate();
TwoPhaseSB sb = ed.getTwoPhaseSB();
sb.testTransaction();
Session Bean(TwoPhaseSB):
public void testTransaction() {
try{
//TestInfo is an entity bean
TestInfoLocal testInfo;
TestInfoLocalHome testInfoHome = TestInfoUtil.getLocalHome();
testInfo = testInfoHome.create("525252", "353535", "transaction test insert", "etc");
testInfo.setElement_id("111111");
//There's no item which has its primary key as 7777, so there's exception error occurs
testInfo = testInfoHome.findByPrimaryKey(new Integer(7777));
}catch(Exception e){
e.printStackTrace();
}
}
Since the code in testTransaction is supposed to run in the same transaction, if there's exception occurrs, isn't it supposed to rollback? I mean, creation of item and update should be rollback. But, the data is inserted and created although there's exception. Could anyone can give me the idea why transaction doesn't work? FYI, I'm using Weblogic8.1.
-
EJB Transaction does not work (1 messages)
- Posted by: jade hwang
- Posted on: October 31 2005 13:02 EST
Threaded Messages (1)
- EJB Transaction does not work by Cristiano Kliemann on October 31 2005 18:44 EST
-
EJB Transaction does not work[ Go to top ]
- Posted by: Cristiano Kliemann
- Posted on: October 31 2005 18:44 EST
- in response to jade hwang
First, to let the container roll back the transaction, you should pass the exception to it. So, if you handle the exception in a try-catch block, you'll need to re-throw it.
Also, the container rolls back the transaction only with RuntimeExceptions. If you want to roll back the transaction after a Exception other than runtime, you must call the session context's setRollbackOnly().
public void testTransaction() {
try{
//TestInfo is an entity bean
TestInfoLocal testInfo;
TestInfoLocalHome testInfoHome = TestInfoUtil.getLocalHome();
testInfo = testInfoHome.create("525252", "353535", "transaction test insert", "etc");
testInfo.setElement_id("111111");
//There's no item which has its primary key as 7777, so there's exception error occurs
testInfo = testInfoHome.findByPrimaryKey(new Integer(7777));
}catch(Exception e){
e.printStackTrace();
// FinderException isn't a RuntimeException
sessionContext.setRollbackOnly();
}
}