hibernate中opensession和getCurrentSession的区别
来源:互联网 发布:算命软件哪个最准最全 编辑:程序博客网 时间:2024/05/17 07:30
1、openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定的session对象
- package cn.kiwifly.view;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- import org.hibernate.classic.Session;
- import cn.kiwifly.util.MySessionFactory;
- public class View {
- public static void main(String[] args) {
- Configuration configuration = new Configuration().configure();
- SessionFactory sf = configuration.buildSessionFactory();
- Session sessionOpen1 = sf.openSession();
- Session sessionOpen2 = sf.openSession();
- Session sessionThread1 = sf.getCurrentSession();
- Session sessionThread2 = sf.getCurrentSession();
- System.out.println(sessionOpen1.hashCode() + "<-------->" + sessionOpen2.hashCode());
- System.out.println(sessionThread1.hashCode() + "<-------->" + sessionThread2.hashCode());
- }
- }
上面代码输出结果:
546579839<-------->1579795854
141106670<-------->141106670
2、openSession不需要配置,而getCurrentSession需要配置
1中代码如果直接运行会报错,要在hibernate.cfg.xml中加入如下代码才行
- <property name="current_session_context_class">thread</property>
这里我们是让session与当前线程绑定,这里的线程范围应该是一次浏览器的会话过程,也就是说打开网站和关闭浏览器这个时间段。
3、openSession需要手动关闭,而getCurrentSession系统自动关闭
openSession出来的session要通过:
- session.close();
4、Session是线程不同步的,要保证线程安全就要使用getCurrentSession
-----------------------------------------------------------------------------------------------------
目前获取Session的时候存在两种方式,openSession和getCurrentSession,如下:
两种方法的区别如下:(1)openSession每次打开都是新的Session,所以多次获取的Session实例是不同的,并且需要人为的调用close方法进行Session关闭。
(2)getCurrentSession是从当前上下文中获取Session并且会绑定到当前线程,第一次调用时会创建一个Session实例,如果该Session未关闭,后续多次获取的是同一个Session实例;事务提交或者回滚时会自动关闭Sesison,无需人工关闭。
使用getCurrentSession时,需要在配置文件中添加如下:
(1)如果使用的是本地事务(JDBC事务)
(2)如果使用的是全局事务(JTA事务)
- Hibernate中openSession 和 getCurrentSession 的区别
- Hibernate 中 openSession()和getCurrentSession() 的区别
- hibernate中 openSession和getCurrentSession的区别
- hibernate中opensession和getCurrentSession的区别
- hibernate中getCurrentSession()和openSession()区别。
- Hibernate SessionFactory中openSession和getCurrentSession方法的区别
- Hibernate SessionFactory中openSession和getCurrentSession方法的区别
- hibernate中openSession()方法和getCurrentSession()方法的区别
- hibernate 的openSession和getCurrentSession的区别
- Hibernate 中getCurrentSession 与 openSession() 的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate之openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate: openSession() 和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别 .
- Android 资源与属性备忘单
- JAVA 延时队列
- Ubuntu Server16安装Jenkins
- 简单的matplotlib用法
- java远程控制tomcat启动关机
- hibernate中opensession和getCurrentSession的区别
- string.ToString(System.IFormatProvider)”最匹配的重载方法具有一些无效参数
- 调研套接字server bind 失败的原因?——Address already in use
- SVM资料
- 【简记】Java Web 内幕——Spring源码(组件分析,BeanFactory源码,Bean创建之前)
- Android移植librtmp——NDK编译JNI调用
- Android Studio 设置 LogCat 的字体大小
- 宇视科技2015应届生招聘笔试题
- 2014 小结