hibernate中opensession和getCurrentSession的区别

本文详细对比了 Hibernate 中 openSession 和 getCurrentSession 的使用方法及其区别,包括它们如何获取 Session 对象、配置需求、线程安全性及关闭机制等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定的session对象

  1. package cn.kiwifly.view;  
  2.   
  3. import org.hibernate.SessionFactory;  
  4. import org.hibernate.cfg.Configuration;  
  5. import org.hibernate.classic.Session;  
  6.   
  7. import cn.kiwifly.util.MySessionFactory;  
  8.   
  9. public class View {  
  10.   
  11.     public static void main(String[] args) {  
  12.   
  13.         Configuration configuration = new Configuration().configure();  
  14.         SessionFactory sf = configuration.buildSessionFactory();  
  15.           
  16.         Session sessionOpen1 = sf.openSession();  
  17.         Session sessionOpen2 = sf.openSession();  
  18.           
  19.         Session sessionThread1 = sf.getCurrentSession();  
  20.         Session sessionThread2 = sf.getCurrentSession();  
  21.           
  22.         System.out.println(sessionOpen1.hashCode() + "<-------->" + sessionOpen2.hashCode());  
  23.         System.out.println(sessionThread1.hashCode() + "<-------->" + sessionThread2.hashCode());  
  24.           
  25.   
  26.     }  
  27.   
  28. }  

上面代码输出结果:

546579839<-------->1579795854
141106670<-------->141106670


2、openSession不需要配置,而getCurrentSession需要配置

1中代码如果直接运行会报错,要在hibernate.cfg.xml中加入如下代码才行

  1. <property name="current_session_context_class">thread</property>  

这里我们是让session与当前线程绑定,这里的线程范围应该是一次浏览器的会话过程,也就是说打开网站和关闭浏览器这个时间段。


3、openSession需要手动关闭,而getCurrentSession系统自动关闭

openSession出来的session要通过:

  1. session.close();  
而getSessionCurrent出来的session系统自动关闭,如果自己关闭会报错


4、Session是线程不同步的,要保证线程安全就要使用getCurrentSession

-----------------------------------------------------------------------------------------------------

目前获取Session的时候存在两种方式,openSession和getCurrentSession,如下:

[java]  view plain  copy
 print ?
  1. <span style="white-space:pre">    </span>Configuration config = new Configuration();  
  2.     SessionFactory sessionFactory = config.buildSessionFactory();  
  3.     //方式一  
  4.     Session session1 = sessionFactory.openSession();  
  5.     //方式二  
  6.     Session session2 = sessionFactory.getCurrentSession();  
两种方法的区别如下:

(1)openSession每次打开都是新的Session,所以多次获取的Session实例是不同的,并且需要人为的调用close方法进行Session关闭。

(2)getCurrentSession是从当前上下文中获取Session并且会绑定到当前线程,第一次调用时会创建一个Session实例,如果该Session未关闭,后续多次获取的是同一个Session实例;事务提交或者回滚时会自动关闭Sesison,无需人工关闭。

使用getCurrentSession时,需要在配置文件中添加如下:

(1)如果使用的是本地事务(JDBC事务)

[html]  view plain  copy
 print ?
  1. <property name="current_session_context_class">thread</property>  
(2)如果使用的是全局事务(JTA事务)

[html]  view plain  copy
 print ?
  1. <property name="current_session_context_class">jta</property>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值