`
fuhuijun
  • 浏览: 31004 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

sessionFactory.openSession()和sessionFactory.getCurrentSession()区别

阅读更多

getCurrentSession ()   使用当前的 session

openSession()         重新建立一个新的 session

 

-------------------------

SessionFactory.getCurrentSession() Hibernate 应用获取 Session 的常用方法。在调用该方法时, Hibernate 会从 interface CurrentSessionContext 获取当前的 Session ,这是 Hibernate 在不同组件中传递 Session 的方法。

 

CurrentSessionContext 有三个实现,分别是 ThreadLocalSessionContext JTASessionContext ManagedSessionContext

 

ThreadLocalSessionContext Session 与当前线程绑定,是使用较多的一种方案;

JTASessionContext Session JTA 事务绑定,在 JTA 环境中使用;

 

ManagedSessionContext 使应用可以通过 bind() unbind() 方法控制 Session 的绑定,主要在有 Conversation 的应用中使用(如果使用 ManagedSessionContext ,开发人员要做的事情还是很多的)。

 

CurrentSessionContext 实现的选择可以通过 hibernate.current_session_context_class 来配置。

 

另一种更常见的创建 Session 的方法是 openSession()

 

 

openSession() getCurrentSession() 有何不同和关联呢?

 

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

 

再说 ManagedSessionContext ,它提供了更灵活的绑定 Session 的方式,但是使用起来去不简单。

Hibernate CaveatEmptor 实例中有关于使用 ManagedSessionContext 的例子,但更好的选择是使用 Seam Framework

分享到:
评论

相关推荐

    SessionFactory.getCurrentSession与openSession的区别

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

    新Hibernate SessionFactory().getCurrentSession()猫腻

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

    HibernateSessionFactory.java

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

    用户管理系统

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

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

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

    三大框架原理

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

    ACCP8.0北大青鸟SSH10章上机

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

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

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

    青鸟租房项目

    session=sessionfactory.openSession(); tran=session.beginTransaction(); //Users user=(Users)session.get(Users.class, 1001);

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

    Springboot调用Oracle存储过程的几种方式.docx

    SSH项目改为Spingboot项目,将项目中部分需要调用存储过程的部分用entityManagerFactory.unwrap(SessionFactory.class).openSession()来获取Session实现后发现项目访问数据库超过十次就会挂掉,原因是Springboot...

    hibernate session.doc

    例如以下代码先加载一个持久化对象,然后通过delete()方法将它删除: Session session1 = sessionFactory.openSession(); Transaction tx1 = session1.beginTransaction(); // 先加载一个持久化对象 Customer ...

    2022年Hibernate下数据批量处理Java教程.docx

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

    使用 HibernateSessionFactory 类

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

    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