`
KIWIFLY
  • 浏览: 5693 次
文章分类
社区版块
存档分类
最新评论

openSession 与 getCurrentSession的区别

 
阅读更多

1、openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定的session对象

package cn.kiwifly.view;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.classic.Session;

import cn.kiwifly.util.MySessionFactory;

public class View {

	public static void main(String[] args) {

		Configuration configuration = new Configuration().configure();
		SessionFactory sf = configuration.buildSessionFactory();
		
		Session sessionOpen1 = sf.openSession();
		Session sessionOpen2 = sf.openSession();
		
		Session sessionThread1 = sf.getCurrentSession();
		Session sessionThread2 = sf.getCurrentSession();
		
		System.out.println(sessionOpen1.hashCode() + "<-------->" + sessionOpen2.hashCode());
		System.out.println(sessionThread1.hashCode() + "<-------->" + sessionThread2.hashCode());
		

	}

}

上面代码输出结果:

546579839<-------->1579795854
141106670<-------->141106670


2、openSession不需要配置,而getCurrentSession需要配置

1中代码如果直接运行会报错,要在hibernate.cfg.xml中加入如下代码才行

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

这里我们是让session与当前线程绑定,这里的线程范围应该是一次浏览器的会话过程,也就是说打开网站和关闭浏览器这个时间段。


3、openSession需要手动关闭,而getCurrentSession系统自动关闭

openSession出来的session要通过:

session.close();
而getSessionCurrent出来的session系统自动关闭,如果自己关闭会报错


4、Session是线程不同步的,要保证线程安全就要使用getCurrentSession




分享到:
评论

相关推荐

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

    NHibernate中的Session示例源代码

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

    castle.activerecord

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

    hibernate经典文档

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

    Java常见面试题208道.docx

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

    OA项目SSH整合框架

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

    用户管理系统

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

    pkcs11 国际标准

    pkcs11 国际标准 C_Inltlize C_Finalze C_GetSlotList C_OpenSession

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

    ssh用户管理系统

    Session session = factory.openSession(); Serializable id = null; Transaction tran = null; try{ tran = session.beginTransaction(); id = session.save(u); tran.commit(); }catch(Exception exp)...

    hibernate session.doc

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

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

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

    三大框架原理

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

    picsart-android-sdk

    您基本上应该在 LoginManager 实例上设置 openSession 并设置 RequestListener,如果返回成功代码,则自动授权成功。 请注意,目前还没有过期的令牌功能。 有关详细信息,请参阅示例应用程序中示例代码片段中的示例...

    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