`
hetongfei
  • 浏览: 842 次
最近访客 更多访客>>
社区版块
存档分类
最新评论

openSession()与getCurrentSession()区别:

阅读更多
来自 http://blog.csdn.net/dy511/article/details/6166134
1.getCurrentSession创建的session会和绑定到当前线程,而openSession不会。
2. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭。
这里getCurrentSession本地事务(本地事务:jdbc)时要在配置文件里进行如下设置
    * 如果使用的是本地事务(jdbc事务)
<property name="hibernate.current_session_context_class">thread</property>
* 如果使用的是全局事务(jta事务)
<property name="hibernate.current_session_context_class">jta</property>
3.getCurrentSession () 使用当前的session,openSession() 重新建立一个新的session
4.在一个应用程序中,如果DAO 层使用Spring 的hibernate 模板,通过Spring 来控制session 的生命周期,则首选getCurrentSession ()。
5.在 SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在 getCurrentSession() 被调用的时候,实际被执行的方法是 CurrentSessionContext.currentSession() 。在 currentSession() 执行时,如果当前 Session 为空, currentSession 会调用 SessionFactory 的 openSession 。所以 getCurrentSession() 对于 Java EE 来说是更好的获取 Session 的方法。
分享到:
评论
1 楼 hetongfei 2014-08-17  
            

相关推荐

    SessionFactory.getCurrentSession与openSession的区别

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

    getCurrentSession 与 openSession() 的区别

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

    hibernate 学习笔记

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

    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 ...

    OA项目SSH整合框架

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

    Spring4.0+Hibernate4.0+Struts2.3整合案例

    Spring4.0+Hibernate4.0+Struts2.3整合案例:实现增删改查。... 可以解决办法是:当方法不需要事务支持的时候,使用 Session session = sessionFactory.openSession()来获得Session对象,问题解决!

    Java常见面试题208道.docx

    123.在 hibernate 中 getCurrentSession 和 openSession 的区别是什么? 124.hibernate 实体类必须要有无参构造函数吗?为什么? 十三、Mybatis 125.mybatis 中 #{}和 ${}的区别是什么? 126.mybatis 有几种分页方式...

    NHibernate中的Session示例源代码

    NHibernate中的Session示例源代码,适合对NHibernate的使用有一定的了解,并想了解NHibernate源代码及其软件结构的人。

    castle.activerecord

    找这个资源的人都知道这是干什么的,就不多说什么了,

    hibernate经典文档

    hibernate 经典文档,学习hibernate 必备的文档,深入浅出,非常实用,强烈推荐!

    hibernate操作数据库笔记

    Session session = SessionFactory对象.openSession(); 注:由于SessionFactory是重量级对象,很耗资源,所以在获取SessionFactory对象时最好用单例模式获取,以确保一个应用中只有一个SessionFactory对象存在和线程...

    Hibernate3使用经验

    ---------------Hibernate3.0 配置-------------- 1.Hibernate中配置参数 /** * 注意:HQL中使用参数的方法: * 1.根据参数名称来设置参数:匹配名称; * 2....Session session = sf.openSession();

    用户管理系统

    Session session=sessionFactory.openSession(); String hql="from User as u where u.username=? and u.userpass=? and u.userright=?"; Query query=session.createQuery(hql) ; query.setString(0, u....

    gamejoltlua:为您所有可爱的Lua项目获得GameJolt的机会

    openSession ()-- your further manipulations 用于GameJolt模块的Haxe风格的界面: interface GameJolt {static var username : String ;static var userToken : String ;static var isLoggedIn : Bool ;static ...

    J2EE利用Hibernate采用B/S架构网页设计

    sessionFactory.openSession() : null; threadLocal.set(session); } return session; } /** * Rebuild hibernate session factory * */ public static void rebuildSessionFactory() { try { ...

    php数据层访问封装

    对php数据访问层进行封装。 $session = SessionFactory::OpenSession(); $session-&gt;create('user',$user); 这样就创建了一个User实体。$user是个array

    Hibernate查询语言

    Session session = sessionFactory.openSession(); Transaction tx = session.beginTransaction(); for ( int i=0; i; i++ ) { Customer customer = new Customer(.....); session.save(customer); if ( i % 20 == 0...

    struts2.3.x+spring3.1.x+hibernate3.6 demo

    关键问题有几个,第一个HibernateDaoSupport这个没有了,在使用hibernateTemplate的时候,报错误:java.lang.NoSuchMethodError: org.hibernate.SessionFactory.openSession()Lorg/hibernate/classic/Session 很是悲...

Global site tag (gtag.js) - Google Analytics