`
anson_xu
  • 浏览: 505698 次
  • 性别: Icon_minigender_1
  • 来自: 惠州
社区版块
存档分类

关于openSession()与getCurrentSession()

    博客分类:
  • SSH
阅读更多

关于openSession()与getCurrentSession()

    在学习Hibernate过程,遇到了疑问,创建session时,有两种不同的方法,尚学堂里用的是openSession();而更多的书的用到的是getCurrentSession();至于写的一个工具类叫做HibernateSessionFactory还是HibernateUtils没有太多的异议,只是命名不同而已。
    网上查了大量资料和hibernate-3.2的API把这两种创建session的差异总结如下:
①getCurrentSession创建的session会和绑定到当前线程,而openSession不会。
②getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭, getCurrentSession的使用可以参hibernate\hibernate-3.2\doc\tutorial\src项目。
这里getCurrentSession本地事务(本地事务:jdbc)时 要在配置文件里进行如下设置
* 如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
getCurrentSession () 使用当前的session
openSession() 重新建立一个新的session

    在一个应用程序中,如果DAO 层使用Spring 的hibernate 模板,通过Spring 来控制session 的生命周期,则首选getCurrentSession ()。
使用Hibernate的大多数应用程序需要某种形式的“上下文相关的” session,特定的session在整个特定的上下文范围内始终有效。然而,对不同类型的应用程序而言,要为什么是组成这种“上下文”下一个定义通常是困难的;不同的上下文对“当前”这个概念定义了不同的范围。在3.0版本之前,使用Hibernate的程序要么采用自行编写的基于 ThreadLocal的上下文session,要么采用HibernateUtil这样的辅助类,要么采用第三方框架(比如Spring或Pico),它们提供了基于代理(proxy)或者基于拦截器(interception)的上下文相关session。

    从3.0.1版本开始,Hibernate增加了SessionFactory.getCurrentSession()方法。一开始,它假定了采用JTA事务,JTA事务定义了当前session的范围和上下文(scope and context)。Hibernate开发团队坚信,因为有好几个独立的JTA TransactionManager实现稳定可用,不论是否被部署到一个J2EE容器中,大多数(假若不是所有的)应用程序都应该采用JTA事务管理。基于这一点,采用JTA的上下文相关session可以满足一切需要。
    在 SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在 getCurrentSession() 被调用的时候,实际被执行的方法是 CurrentSessionContext.currentSession() 。在 currentSession() 执行时,如果当前 Session 为空, currentSession 会调用 SessionFactory 的 openSession 。所以 getCurrentSession() 对于 Java EE 来说是更好的获取Session 的方法。
   
    那么跟跟openSession相比,getCurrentSession在使用上有什么注意的呢? 到现在发现的一个就是,由于getCurrentSession方法返回的session在做事务的commit时, session可能会自动给关掉,这样若自己的代码中再调用session.close时就抛出了"Session was already closed"异常。

原来在做jbpm工作流时老是出现session的问题出现异常,算是办法也简单session.close(),原因是我在sevice层 openSession而忘记关闭,每写一个方法都必须这样。而采用getCurrentSession的话就不需要了,现总结两者间的区别:

1. 如果使用的是getCurrentSession来创建session的话,在commit后,session就自动被关闭了, 也就是不用再session.close()了。但是如果使用的是openSession方法创建的session的话,那么必须显示的关闭session,也就是调用session.close()方法。这样commit后,session并没有关闭。
2.  使用SessionFactory.getCurrentSession()需要在hibernate.cfg.xml中如下配置:
   * 如果采用jdbc独立引用程序配置如下:
      <property name="hibernate.current_session_context_class">thread</property>
   * 如果采用了JTA事务配置如下 
      <property name="hibernate.current_session_context_class">jta</property>

另加:

HB3.X 版本中提供了一个getCurrentSession() 这个方法,这个方法和早期使用的openSession() 是有区别的。
openSession()
,表示创建了一个新的session 对象,当你使用完了以后就要必须调用close 方法来关闭当前的session
getCurrentSession()
,总是会返回 当前的 工作单元。Session 在第一次被使用的时候,即第一次调用getCurrentSession() 的时候,其生命周期就开始。然后她被Hibernate 绑定到当前线程。当事物结束的时候,不管是提交还是回滚,Hibernate 会自动把Session 从当前线程剥离,并且关闭。若在次调用getCurrentSession() ,会得到一个新的Session, 并且开始一个新的工作单元。这是Hibernate 最广泛的thread-bound model ,支持代码灵活分层( 事物划分和数据访问代码的分离)
但是使用他的时候有2 种配置:1JDBC 的配置<property name="hibernate.current_session_context_class">thread</property> 这个是表示采用jdbc 独立引用。
2
JTA 的配置<property name="hibernate.current_session_context_class">jta</property>
本人建议使用getCurrentSession() 更好这样就不用使用close 来关闭你的session

分享到:
评论

相关推荐

    SessionFactory.getCurrentSession与openSession的区别

    博文链接:https://shaqiang32.iteye.com/blog/201918

    getCurrentSession 与 openSession() 的区别

    NULL 博文链接:https://bbxyhaihua.iteye.com/blog/505085

    ssh中getCurrentSession的使用

    2. **为什么使用getCurrentSession()**:与直接调用`openSession()`创建新的Session相比,`getCurrentSession()`有以下优势: - 它能够自动管理Session的生命周期,比如在请求结束时关闭Session,避免资源泄露。 -...

    SSH框架部分常见面试题.pdf

    openSession 和 getCurrentSession 是 Hibernate 中的两个会话方法,openSession 用于创建新的会话,getCurrentSession 用于获取当前会话。 saveOrUpdate() 和 merge() saveOrUpdate() 和 merge() 是 Hibernate 中...

    hibernate关于session的关闭实例解析

    本文详细介绍了 Hibernate 中 Session 的关闭实例解析,包括 getSession() 和 openSession() 的区别、getCurrentSession() 的配置、openSession() 和 getCurrentSession() 的关联,以及 Session 的关闭。希望本文...

    J2EE考试题目及答案定义.pdf

    openSession()和getCurrentSession()方法都是用于获取Session对象的方法,但它们有所不同。getCurrentSession创建的Session对象会和当前线程绑定,而openSession不会。getCurrentSession创建的Session对象在事务...

    SSH-Jsp期末考试简答题带答案

    openSession()和getCurrentSession()是Hibernate中两个重要的Session创建方法。采用getCurrentSession()创建的Session会绑定到当前的线程中去,而采用openSession()则不会。getCurrentSession()创建的Session在...

    J2EE考试题目及答案.doc

    `getCurrentSession()`创建的Session与当前线程绑定,事务结束后自动关闭,而`openSession()`则不会自动管理,需要手动关闭。`getCurrentSession()`更适用于需要在同一个事务中操作多个对象的情况。 7. **Session...

    hibernate 学习笔记

    hibernate 学习笔记: 了解hibernate的基本概念 配置hbm.xml cfg.xml 快速入门案例3: 从domain-xml-数据库表 ...openSession()和getCurrentSession() 线程局部变量模式 transaction事务 在web项目中开发hibernate

    hibernate笔记心得

    在Hibernate中,我们有两种方式来获取和管理Session:`openSession()`和`getCurrentSession()`。`openSession()`每次都会创建一个新的Session,并且在使用完毕后需要手动关闭,这适用于短生命周期的事务。而`...

    JAVAEE试题(含答案).pdf

    6. Hibernate 框架中的 SessionFactory 类有两个获得 session 的方法:openSession() 和 getCurrentSession()。如果需要在同一线程中,保证使用同一个 Session 则使用 getCurrentSession(),如果在一个线程中,需要...

    hibernate学习笔记

    - 提供`openSession()`和`getCurrentSession()`方法: - `openSession()`每次创建新Session,使用后需关闭。 - `getCurrentSession()`提供线程绑定的Session,方便事务管理。 总结,Hibernate通过O/R Mapping...

    JAVAEE试题(含答案).docx

    6. Hibernate框架中的SessionFactory类:SessionFactory类提供了两个获得session的方法,分别是openSession()和getCurrentSession()。这两个方法的区别在于,openSession()方法可以在一个线程中使用不同的Session,...

    hibernate核心知识

    - `getCurrentSession()`:获取与当前线程绑定的Session。在一个线程中,多次调用`getCurrentSession()`会返回相同的Session。 5. **Session接口**:Session代表与数据库的一次会话,它是线程不安全的,每次操作...

    Java面试宝典2020修订版V1.0.1.doc

    9、openSession和getCurrentSession 90 10、拦截器的作用?拦截器和过滤器的区别? 91 11、struts.xml中result的type有哪些类型? 91 12、什么时候用JDBC什么时候用Hibernete; 91 13、hibernate 数据的三个状态 91 ...

    hibernate学习教程

    - Session的获取:通过SessionFactory的openSession()或getCurrentSession()方法。 - CRUD操作:使用Session的save(), saveOrUpdate(), get(), update(), delete()等方法。 - 查询:使用Criteria API或HQL进行...

    Spring与Hibernate集成中的session

    - 创建`Session`:`SessionFactory.openSession()`或`SessionFactory.getCurrentSession()`(在已配置的事务环境下)。 - 事务开始:`Session.beginTransaction()`。 - 数据操作:如`Session.save()`, `Session....

    OA项目SSH整合框架

    Assert.assertNotNull(sessionFactory.openSession()); } 2,配置声明式事务(使用基于注解的方式) 1,配置 &lt;!-- 配置事务管理器 --&gt; &lt;property name="sessionFactory" ref="sessionFactory"&gt;...

Global site tag (gtag.js) - Google Analytics