Hibernaate事务管理

Hibernate使用session时需要继承HibernateDaoSupport对象

HibernateDaoSupport对象中包含默认的getSession()方法,但不可以通过该方法直接启动和提交事务

错误写法:(这样开启事务会报Transaction not successfully started,事务没有启动异常)

个人理解:getSession()每次hibernate都会new一个新的session对象,所以当提交时会出现事物没有启动异常

getSession.getTransaction().begin();

getSession.saveOrUpdate(accountInfo);

getSession.getTransaction().commit();

正确的写法:

Session session = getSession();

session.getTransaction().begin();

session.saveOrUpdate(accountInfo);

session.getTransaction().commit();

posted @ 2014-03-19 10:30  IT Daren  阅读(156)  评论(0编辑  收藏  举报