来源(转载):http://blog.csdn.net/woshisap/article/details/7024482

1:getCurrentSession会把Session和当前的线程关联起来,而openSession只是重新开启一个Session

2:getCurrentSession获得的Session会在事务关闭或者回滚时会自动关闭,而openSession获得的Session必须手动关闭

getCurrentSession,特定的实现用它来负责跟踪当前的上下文session,Hibernate内置了此接口的两种实现

* org.hibernate.context.JTASessionContext --当前session通过当前执行的线程来跟踪和界定

* org.hibernate.context.ThreadLocalSessionContext --当前线程通过当前执行的线程和跟踪和界定

这两种实现都提供了“每数据库事务对应一个session”的编程模型,也称作每次请求一个session,Hibernate session的起始和终结由数据库事务的生存来控制。

3:关于ThreadLocal的理解

线程局部变量,就是为每一个使用某变量的线程都提供一个该变量值的副本,是每一个线程都可以独立的改变自己的副本,而不会和其他线程的副本冲突,从线程的

角度看就好像每一个线程都完全拥有一个变量

实例代码:

  1. package com.capinfotech.ju.util;
  2. import org.hibernate.HibernateException;
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.cfg.Configuration;
  6. public class HibernateUtil {
  7. public static final ThreadLocal sessionThreadLoacl = new ThreadLocal();
  8. public static final SessionFactory sessionFactory;
  9. static {
  10. try {
  11. sessionFactory = new Configuration().configure().buildSessionFactory();
  12. } catch(Throwable t) {
  13. throw new ExceptionInInitializerError(t);
  14. }
  15. }
  16. /**
  17. * 获取当前的Session
  18. * @return
  19. * @throws HibernateException
  20. */
  21. public static Session currentSession() throws HibernateException {
  22. Session session = (Session) sessionThreadLoacl.get();
  23. if(session == null) {
  24. session = sessionFactory.openSession();
  25. sessionThreadLoacl.set(session);
  26. }
  27. return session;
  28. }
  29. /**
  30. * 释放Session
  31. * @throws HibernateException
  32. */
  33. public static void closeSession() throws HibernateException {
  34. Session session = (Session)sessionThreadLoacl.get();
  35. if(session != null) {
  36. session.close();
  37. }
  38. sessionThreadLoacl.set(null);
  39. }
  40. }
  1. package com.capinfotech.ju.util;
  2.  
  3. import org.hibernate.HibernateException;
  4. import org.hibernate.Session;
  5. import org.hibernate.SessionFactory;
  6. import org.hibernate.cfg.Configuration;
  7.  
  8. public class HibernateUtil {
  9.  
  10. public static final ThreadLocal sessionThreadLoacl = new ThreadLocal();
  11.  
  12. public static final SessionFactory sessionFactory;
  13.  
  14. static {
  15. try {
  16. sessionFactory = new Configuration().configure().buildSessionFactory();
  17. } catch(Throwable t) {
  18. throw new ExceptionInInitializerError(t);
  19. }
  20. }
  21.  
  22. /**
  23. * 获取当前的Session
  24. * @return
  25. * @throws HibernateException
  26. */
  27. public static Session currentSession() throws HibernateException {
  28. Session session = (Session) sessionThreadLoacl.get();
  29. if(session == null) {
  30. session = sessionFactory.openSession();
  31. sessionThreadLoacl.set(session);
  32. }
  33. return session;
  34. }
  35.  
  36. /**
  37. * 释放Session
  38. * @throws HibernateException
  39. */
  40. public static void closeSession() throws HibernateException {
  41. Session session = (Session)sessionThreadLoacl.get();
  42. if(session != null) {
  43. session.close();
  44. }
  45.  
  46. sessionThreadLoacl.set(null);
  47.  
  48. }
  49. }

在sessionFactory启动的时候,Hibernate会根据配置创建相应的CurrentSessionContext,在getCurrentSession()被调用的时候,实际被执行的方法是CurrentSessionContext.currentSession(),在currentSession()执行时,如果当前Session为空,currentSession会调用SessionFactory的openSession,

这样既保证了Session的线程安全,又不用每次数据库操作都重新获取一个Session实例,实现了Session重用

Hibernate关于openSession和getCurrentSession的理解的更多相关文章

  1. HIbernate中openSession和getCurrentSession

      这两者的差别网上非常多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题.   openSession和getCurrentSession的根本差别在于有没有绑定当前线程,所以,用法有差 ...

  2. hibernate中openSession()跟getCurrentSession()方法之间的区别

    Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...

  3. Hibernate中openSession() 与 getCurrentSession()的区别

    1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...

  4. Hibernate之openSession与getCurrentSession的区别

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

  5. hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别

    1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...

  6. openSession和getCurrentSession的比较

    在比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法. 在进行配置信息管理时,我们一般进行一下简单步骤: Configuration cfg = n ...

  7. sessionFactory中的openSession和getCurrentSession的一些注意事项

    今天进行Hibernate测试时遇到了一个问题 我在用sessionFactory生产seesion时出现了故障,使用getCurrentsesstion时产生异常: Exception in thr ...

  8. openSession()与getCurrentSession()的区别

    getCurrentSession创建的session会和绑定到当前线程,而openSession不会. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSes ...

  9. hibernate3.6-联合主键注解以及openSession和getCurrentSession区别

    [联合主键]>>>>配置方式:xml:    1. Student中单独创建StudentPk主键实体类 2. 配置: <composite-id name=" ...

随机推荐

  1. ios隐藏软键盘

    //判断是否为苹果 var isIPHONE = navigator.userAgent.toUpperCase().indexOf('IPHONE')!= -1; // 元素失去焦点隐藏iphone ...

  2. Java中的使用了未经检查或不安全的操作

    1. javac -Xlint:unchecked MyMath.java检查不安全的操作 /***************************************************/ ...

  3. 2016WHD.china世界云计算日·北京站即将召开

    WHD.china自进驻中国以来,已在上海.北京成功举办多届,2015年于北京举办的会议更是盛况空前,注册参会者逾800人次,汇聚了国内外众多知名云服务商.IDC商.电子商务企业.电信运营商.ISP等 ...

  4. doc 窗口操作图

    doc 窗口操作图doc 窗口操作图vdoc 窗口操作图

  5. C# Monads的实现(二)

    再谈continuation monad 上一篇中我们已经介绍了continuation monad,但是这个monad与Identity,Maybe,IEnumerable monads稍微难于理解 ...

  6. ESFramework 4.0 进阶(01)-- 消息

    需要交互的分布式系统之间通过消息来传递有意义的信息.消息是通信框架的核心.离开了消息,再谈通信框架就没有任何意义,所以,消息是ESFramework中一个最核心的概念. 一. 消息的类别 在具体的应用 ...

  7. RFC3489 STUN之客户端所处环境探测流程与部分属性含义说明

    1 STUN客户端所处环境探测流程 1.1 流程图 1.2 流程图中Binding请求类型说明 类型1:Binding请求消息中不设置CHANGE-REQUEST,或若设置其相应更改IP与端口标志位都 ...

  8. stm32菜单按键的设计

    有点懒.看注释吧 // k0,enter/enable;k3:esc/disable// k1,value+/menu+;k2:menu-/value-; #include "sysmenu ...

  9. JPA 系列教程16-继承-联合子类-JOINED

    联合子类策略 这种情况下子类的字段被映射到各自的表中,这些字段包括父类中的字段,并执行一个join操作来实例化子类. 举例 如果实体类Teacher继承实体类Person,实体类Student也继承自 ...

  10. postgresql 修改属性

    up vote2down votefavorite From this article, I tried to update or delete property of a JSONB column: ...