`

hibernate中SessionFactory.opensession()与getcurrentsession()的区别

    博客分类:
  • J2EE
阅读更多
1. getCurrentSession创建的session会和绑定到当前线程,而openSession不会。

2. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSession必须手动关闭

3. 注意:1,使用 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>

4.openSession() 与 getCurrentSession()不同与关联:
在 SessionFactory 启动的时候, Hibernate 会根据配置创建相应的 CurrentSessionContext ,在 getCurrentSession() 被调用的时候,实际被执行的方法是 CurrentSessionContext.currentSession() 。在 currentSession() 执行时,如果当前 Session 为空, currentSession 会调用 SessionFactory 的 openSession 。所以 getCurrentSession() 对于 Java EE 来说是更好的获取 Session 的方法。

5.另外在应用程序中,如果 DAO 层使用 Spring 的 hibernate 模板,通过 Spring 来控制 session 的生命周期,则应该 getCurrentSession ()。
分享到:
评论

相关推荐

    SessionFactory.getCurrentSession与openSession的区别

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

    新Hibernate SessionFactory().getCurrentSession()猫腻

    NULL 博文链接:https://zgdkik.iteye.com/blog/1835667

    HibernateSessionFactory.java

    MyEclipse生成的HibernateSessionFactory.java文件,应该是完整的。

    HibernateSessionFactory.java Hibernate使用的整合的工具文件

    Session s= HibernateSessionFactory.getSession(); 就是Hibernate的工具java类

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

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

    三大框架原理

    2.由hibernate.cfg.xml中的&lt;mapping resource="com/xx/User.hbm.xml"/&gt;读取并解析映射信息 3.通过config.buildSessionFactory();//创建SessionFactory 4.sessionFactory.openSession();//打开Sesssion 5.session....

    用户管理系统

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

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

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

    hibernate.properties

    hibernate.properties # # Hibernate, Relational Persistence for Idiomatic Java # # License: GNU Lesser General Public License (LGPL), version 2.1 or later. # See the lgpl.txt file in the root directory...

    ACCP8.0北大青鸟SSH10章上机

    Configuration configuration = new ...或者通过MyEclipse的Hibernate工具自动生成的HibernateSessionFactory.getSession()方法来获取Session,调用HibernateSessionFactory. closeSession()方法来关闭session。

    hibernate session.doc

    delete()方法用于从数据库中删除与Java对象对应的记录。如果传入的参数是持久化对象,Session就计划执行一个delete语句。如果传入的参数是游离对象,先使游离对象被Session关联,使它变为持久化对象,然后计划执行一...

    使用 HibernateSessionFactory 类

    NULL 博文链接:https://chaoyi.iteye.com/blog/2148991

    hibernate中的SessionFactory

    hibernate中的SessionFactoryhibernate中的SessionFactoryhibernate中的SessionFactory

    Hibernate中的sessionFactory

    Hibernate中的sessionFactory

    SSH的jar包.rar

    4.sessionFactory.openSession();//打开Sesssion 5.session.beginTransaction();//创建事务Transation 6.persistent operate持久化操作 7.session.getTransaction().commit();//提交事务 8.关闭Session 9.关闭...

Global site tag (gtag.js) - Google Analytics