Rhyme/Hibernate getCurrentSession()与openSession()的区别
2017-12-22 17:26
477 查看
Hibernate getCurrentSession()与openSession()的区别
sessionFactory.getCurrentSession()会查看当前上下文环境中有没有session,没有则创建,,在这个session没有提交之前,获得的session永远是同一个session,session一旦提交,则会获取新的session,另外使用getCurrentSession()获得的session不用手动执行session.close()方法另外使用getCurrentSession()方法需要指定应用的上下文,具体配置如下图
而sessionFactory.openSession()每次调用都会创建一个新的session,所以不建议使用
为什么这么做?
原因是,openSession()可能导致事务方法出现不一致的情况
当我在执行事务控制的时候,分别调用两个DAO中的方法,且这两个方法是同步的,这个时候,如果两个DAO中分别使用的都是openSession()方法,那么就不能做到addUser()这个方法的事物一致性。相关文章推荐
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
- 【Hibernate】SessionFactory中的getCurrentSession()和openSession()的区别
- (hibernate之一)Sessionfactory的getCurrentSession与openSession的区别
- Hibernate的getCurrentSession()和openSession()的区别和使用
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
- hibernate中 openSession和getCurrentSession的区别
- hibernate 的openSession和getCurrentSession的区别
- hibernate中关于SessionFactory对象中opensession()和getCurrentSession()的区别
- hibernate中openSession()跟getCurrentSession()方法之间的区别
- Hibernate中的openSession(),getSession() 和 getCurrentSession() 的区别
- Hibernate 中的 openSession和getCurrentSession 方法的区别
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
- Hibernate中getCurrentSession()与openSession()的区别及应用
- getCurrentSession 与 openSession() 的区别及hibernate lazy加载异常
- Hibernate getCurrentSession()和openSession()的区别
- Hibernate getCurrentSession 与 openSession() 的区别
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
- Hibernate各类概念-openSession与getCurrentSession的区别
- Hibernate各类概念-openSession和getCurrentSession的区别
- Hibernate 之getCurrentSession 与 openSession() 的区别