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

openSession 与 getCurrentSession的区别的更多相关文章

  1. Hibernate之openSession与getCurrentSession的区别

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

  2. Hibernate中openSession() 与 getCurrentSession()的区别

    1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...

  3. openSession()与getCurrentSession()的区别

    getCurrentSession创建的session会和绑定到当前线程,而openSession不会. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSes ...

  4. hibernate中openSession()跟getCurrentSession()方法之间的区别

    Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...

  5. hibernate3.6-联合主键注解以及openSession和getCurrentSession区别

    [联合主键]>>>>配置方式:xml:    1. Student中单独创建StudentPk主键实体类 2. 配置: <composite-id name=" ...

  6. openSession和getCurrentSession的比较

    在比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法. 在进行配置信息管理时,我们一般进行一下简单步骤: Configuration cfg = n ...

  7. sessionFactory中的openSession和getCurrentSession的一些注意事项

    今天进行Hibernate测试时遇到了一个问题 我在用sessionFactory生产seesion时出现了故障,使用getCurrentsesstion时产生异常: Exception in thr ...

  8. HIbernate中openSession和getCurrentSession

      这两者的差别网上非常多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题.   openSession和getCurrentSession的根本差别在于有没有绑定当前线程,所以,用法有差 ...

  9. SessionFactory的openSession与getCurrentSession区别

    SessionFactory 1 用来产生和管理sesssion 2 通常情况下,每个应用只需要一个SessionFactory,除非要访问多个数据库的情况 3 openSession()与openS ...

随机推荐

  1. 努力做一个优秀的programmer [ C# 影院售票系统]

    Cinema.cs类 [Serializable] // 电影院类 public class Cinema { public Cinema() { //二进制 SoldTickets = new Li ...

  2. Vue中beforeRouterEnter的应用

    一般判断从哪个页面进入时需要判断路由,用到了beforeRouteEnter方法. 注意:在在内部获取不到外部的this,方法.变量等都获取不到.但是vm.XXXXX可以获取到 beforeRoute ...

  3. Ubuntu下Mongo的安装和笔记

    在linux下的安装 打开https://www.mongodb.com/download-center#community选择linux然后选择自己的Version复制DOWNLOAD旁边的链接 打 ...

  4. 【iOS】手动抛出异常

    之前没遇到过需要手动抛出异常的时候,这次见到了,记录一下.示例代码如下: /** 如果调用 [[BNRItemStore alloc] init],就提示应该使用 [BNRItemStore shar ...

  5. windows server 2008 R2中建立ftp站点

    在windows server 2008 R2中建立ftp站点,要遵循以下步骤: (1) 开启IIS中的ftp服务: (2) 在IIS中建立ftp站点. 具体过程如下: (1) 开启IIS中的ftp服 ...

  6. 七牛云qshell工具定时备份空间文件到本地

    qshell 是利用七牛文档上公开的 API实现的一个方便开发者测试和使用七牛API服务的命令行工具,使用该工具可以实现很多的功能,今天就分享一下利用qshell定时备份空间文件到本地 1.下载qsh ...

  7. Pandas随机采样

    实现对DataFrame对象随机采样 pandas是基于numpy建立起来的,所以numpy大部分函数可作用于DataFrame和Series数据结构. numpy.random.permutatio ...

  8. [Short-Circuit Constraint Violation]警告解决办法

    今天用Altium Designer16画PCB时候遇到一个问题,进行DRC检测,警告如下: [Short-Circuit Constraint Violation] GrayscaleSensor1 ...

  9. React-Native之打包发布(Android)

    React-Native之打包发布(Android) 一,介绍与需求 移动端打包发布到应用市场 二,发布配置 注意:以下所有操作都在win10下进行,React Native版本0.59.5,andr ...

  10. [转] java开源游戏

    收藏一下   triplea  Triplea是一个开放源码的boardgame.它允许玩家选择各种各样的战略版图游戏(如:轴心国或同盟军).TripleA引擎支持联网对战,支持声音,支持使用XML文 ...