zoukankan      html  css  js  c++  java
  • Hibernate中的GetCurrentSession()方法

    从3.0.1版本开 始,Hibernate增加了SessionFactory.getCurrentSession()方法。

    采用getCurrentSession()创建的session在commit或rollback时会自动关闭,如果commit()之后再关闭,就会报session已经关闭的错误;但是如果不commit()而执行关闭,虽然不会报什么错误,但是这时观察数据库添加数据是没有成功的,而openSession必须手动关闭。

    在一个应用程序中,如果DAO 层使用Spring来控制session 的生命周期,则首选getCurrentSession()。

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

    getCurrentSession配置:

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

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

    如果使用的是全局事务(jta事务)

    <property name="hibernate.current_session_context_class">jta</property>
  • 相关阅读:
    第二次结对编程作业
    5 线性回归算法
    4 K均值算法--应用
    3 K均值算法
    2 机器学习相关数学基础
    1 机器学习概述
    15. 语法制导的语义翻译
    14.算符优先分析
    13.自下而上语法分析
    12.实验二 递归下降语法分析
  • 原文地址:https://www.cnblogs.com/wdnnccey/p/5982540.html
Copyright © 2011-2022 走看看