public class HibernateTestBean implements SessionBean {
SessionContext sessionContext;
SessionFactory sf;
public void setSessionContext(SessionContext sessionContext) {
this.sessionContext = sessionContext;
try {
InitialContext ctx = new InitialContext();
sf=(SessionFactory) ctx.lookup("java:/hibernate/SessionFactory");
} catch (NamingException ex) {
ex.printStackTrace();
}
}
public void tran(){
tran1();
tran2();
}
public void tran1() {
Session session=sf.getCurrentSession();
Message msg=new Message();
msg.setCreateTime(new Date());
msg.setDetail("trans1");
session.save(msg);
System.out.println("Session:"+session.hashCode());
session.flush();
session.close();
}
public void tran2() {
Session session=sf.getCurrentSession();
Message msg=new Message();
msg.setCreateTime(new Date());
msg.setDetail("trans2");
session.save(msg);
System.out.println("Session:"+session.hashCode());
// throw new RuntimeException("wrong");
}
……
}
注:EJB采用CMT,各方法的事務屬性是required
客戶端調用tran以上代碼可以正確運行嗎?
如果把tran1中的sf.getCurrentSession();改為sf.openSession()可以正確運行嗎?
辨析:
1 上述代碼是不能正確運行的,運行tran2時會拋出異常,告訴你session is closed.
其實這是應為getCurrentSession()會使用環境已有的Session,同時注意getCurrentSession()要在事務的環境中使用。
這是也許你一定會問,那么什么時候關閉Session呢?答案是事務完成的時候(提交或是回滾)。
2 如果上述代碼tran1中的sf.getCurrentSession();改為sf.openSession()代碼將可以正確運行。這是因為openSession()每次都會返回一個新的Session。而在tran2種的sf.getCurrentSession()并不會使用tran1中的session,而是會使用當前事務環境中的默認的session.
也許你會問如果tran2種的調用拋出RuntimeException,tran1所作的操作還可以回滾嗎?
答案是仍然可以回滾的.
蔡超
北京天融信 軟件架構師
SCEA,SCBCD,MCSD
IBM Certified Specialist RUP v2003
IBM Certified Solution Designer OOA&D UMLv2