Hibernate openSession()和getCurrentSession()的区别
来源:互联网 发布:淘宝微信商城开发 编辑:程序博客网 时间:2024/04/30 04:56
Hibernate openSession()和getCurrentSession()的区别
一、使用方法(基于Hibernate 4.2)
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
Session s1 = sessionFactory.getCurrentSession();
Session s2 = sessionFactory.openSession();
二、比较openSession()和getCurrentSession()的区别
1. 采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建的session则不会。
openSession()是打开一个新的session对象,如果连续使用多次,获得的session并不是同一个对象。而对于getCurrentSession(),第一次使用时会产生一个session对象,而在连续使用多次时,得到的session都是同一个对象。
2. 采用getCurrentSession()创建的session在commit()或rollback()时会自动关闭,而采用openSession()创建的session必须手动关闭。
3. 使用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
- hibernate 的openSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate中openSession 和 getCurrentSession 的区别
- Hibernate 中 openSession()和getCurrentSession() 的区别
- Hibernate openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate之openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate: openSession() 和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别 .
- Hibernate的openSession和getCurrentSession区别
- hibernate中 openSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- 【Hibernate】OpenSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate getCurrentSession()和openSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- C++学习笔记-类4
- 基于http协议的C语言客户端代码
- 静态导入
- 设计模式之观察者模式(5)
- 基于内容的图像检索系统常用特征简介
- Hibernate openSession()和getCurrentSession()的区别
- 跨域Ajax解决方案
- Remove Duplicates from Sorted Array II
- Head First 设计模式 (二) 观察者模式(observer pattern) C++实现
- make: execvp: /opt/EmbedSky/u-boot-1.1.6/mkconfig: 权限不够
- 黑马程序员-抽象类 模板方法和接口
- 我为什么回家:一位程序员老爸的宣言
- android模拟器无法使用camera拍照
- MyEclipse 格式设置