`

Hibernate 事务管理

阅读更多

今日

方法

deleteVtlDir(vtlID, session);中使用递归删除,期望使用事务管理。

使用事物时:

 

Session session = HibernateSessionFactory.getSession();

 

 

出现错误。Can not open session

 

改成代码如下:


  //get session
  Session session = HibernateSessionFactory.getSessionFactory().openSession();
  Transaction tra = session.beginTransaction();
  
  try {
   deleteVtlDir(vtlID, session);
   tra.commit();
  } catch (Exception e) {
   tra.rollback();
   throw new HibernateException(e);
  } finally {
   HibernateSessionFactory.closeSession();
  }

 

运行成功!

在Action之中使用 try{}catch(){}捕捉抛出的异常 throw new  HibernateException(e);
完成!
  

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics