使用HibernateUtil类实现事务处理,代码如下:
StudentDAOImp.javapublic class StudentDAOImp implements StudentDAO {public boolean updateStudent(Student student) {
try{
Session s = HibernateUtil.currentSession();
HibernateUtil.beginTransaction();
s.update(student);
HibernateUtil.commitTransaction();
HibernateUtil.closeSession();
return true;
}catch (HibernateException e){
log.fatal(e);
}
return false;
}
……
}
public static void commitTransaction() {
Transaction tx = (Transaction) tLocaltx.get();
try {
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
{
System.out.println("commit1");
tx.commit();
System.out.println("commit2");
}
tLocaltx.set(null);
System.out.println("commit tx"); } catch (HibernateException e) {
//throw new InfrastructureException(e);
}
}
Console只打印出了commit1,没有commit2和commit tx,怎么回事呢?