oenSession()和 getCurrentSession()的区别
来源:互联网 发布:贵阳大数据学校招生 编辑:程序博客网 时间:2024/05/21 18:30
Configuration cfg = new Configuration();
SessionFactory sf = cfg.configure().buildSessionFactory();
Session session = sf.openSession()
这里使用oenSession()和 getCurrentSession()的区别是:
1、采用getCurrentSession()创建的session会绑定到当前线程中,而采用openSession()创建的session则不会
采用getCurrentSession()创建的session在commit或rollback时会自动关闭,而采用openSession()创建的session必须手动关闭
采用getCurrentSession()创建的session: 如果当前的上下文环境里没有session就创建一个新的,如果有就不用在创建了,拿现有的。而oenSession()永远是创建新的
session,
采用getCurrentSession()创建的session: session提交前,无论拿多少次,都是同一个,提交后,就不是同一个session了,且session无需关闭,会自动close 。用来界
定事务边界。 oenSession() 需要手动close掉。
2、用getCurrentSession()需要在hibernate.cfg.xml文件中加入如下配置:
如果使用的是本地事务(jdbc事务)
<property name="current_session_context_class">thread</property>
如果使用的是全局事务(jta事务)
<property name="current_session_context_class">jta</property>
3、如果不加的话,会出现 异常“No CurrentSessionContext configured”
- oenSession()和 getCurrentSession()的区别
- openSession 和getCurrentSession的区别
- getCurrentSession()和openSession()的区别
- getCurrentSession 和 openSession的区别
- openSession 和getCurrentSession的区别
- OpenSession和GetCurrentSession的区别
- openSession 和getCurrentSession的区别
- OpenSession()和getCurrentSession的联系和区别
- hibernate 的openSession和getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernat中getCurrentSession 和openSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate中openSession 和 getCurrentSession 的区别
- openSession和getCurrentSession的区别 thred jta
- Hibernate 中 openSession()和getCurrentSession() 的区别
- Hibernate openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate之openSession()和getCurrentSession()的区别
- mysql变量使用总结
- iphone4 电话截获
- Java中将JTable中的数据导出到Excel中
- gvim字体设置、背景设置、保存为默认状态
- 找出数组中第k大小的数,输出数所在的位置
- oenSession()和 getCurrentSession()的区别
- BREW怎样支持全局变量和静态变量
- android makefile(android.mk)分析(序)
- D3DXCreateTextureFromFileEx中参数介绍
- qt4环境变量
- qt4程序执行流程
- ubuntu mysql c api的使用开发实例程序。
- 20个公司绝对不会告诉你的秘密
- 献10款chart图表JS插件