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
openSession 与 getCurrentSession的区别的更多相关文章
- Hibernate之openSession与getCurrentSession的区别
openSession 与 getCurrentSession的区别(1)openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定 ...
- Hibernate中openSession() 与 getCurrentSession()的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...
- openSession()与getCurrentSession()的区别
getCurrentSession创建的session会和绑定到当前线程,而openSession不会. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSes ...
- hibernate中openSession()跟getCurrentSession()方法之间的区别
Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...
- hibernate3.6-联合主键注解以及openSession和getCurrentSession区别
[联合主键]>>>>配置方式:xml: 1. Student中单独创建StudentPk主键实体类 2. 配置: <composite-id name=" ...
- openSession和getCurrentSession的比较
在比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法. 在进行配置信息管理时,我们一般进行一下简单步骤: Configuration cfg = n ...
- sessionFactory中的openSession和getCurrentSession的一些注意事项
今天进行Hibernate测试时遇到了一个问题 我在用sessionFactory生产seesion时出现了故障,使用getCurrentsesstion时产生异常: Exception in thr ...
- HIbernate中openSession和getCurrentSession
这两者的差别网上非常多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题. openSession和getCurrentSession的根本差别在于有没有绑定当前线程,所以,用法有差 ...
- SessionFactory的openSession与getCurrentSession区别
SessionFactory 1 用来产生和管理sesssion 2 通常情况下,每个应用只需要一个SessionFactory,除非要访问多个数据库的情况 3 openSession()与openS ...
随机推荐
- 模拟器无Back、Menu等键
问题如图所示: 解决方法: 1. 打开Android Virtual Device (AVD) Manager --> 选择模拟器,并点击edit --> 勾选KeyBoard中的选项,并 ...
- Github上fork的项目如何merge原Git项目
问题场景 小明在Github上fork了一个大佬的项目,并clone到本地开发一段时间,再提交merge request到原Git项目,过了段时间,原作者联系小明,扔给他下面这幅截图并告知合并处理冲突 ...
- HC-08 BLE资料
1.1 特点简介 HC-08蓝牙串口通信模块是新一代的基于Bluetooth Specification V4.0 BLE蓝牙协议的数传模块.无线工作频段为 2.4GHz ISM,调制方式是 GFSK ...
- 使用verilog编写锁存器与触发器
需要注意的地方有四点: 1.关于锁存器与触发器在原理上的不同点,以及代码的不同点 2.关于高电平有效与低电平有效之前的区别 3.理解实现复位与实现D触发器之间的区别 4.理解同步与异步之间的区别 锁存 ...
- .NET中的值类型与引用类型
.NET中的值类型与引用类型 这是一个常见面试题,值类型(Value Type)和引用类型(Reference Type)有什么区别?他们性能方面有什么区别? TL;DR(先看结论) 值类型 引用类型 ...
- C#下载文件,Stream 和 byte[] 之间的转换
stream byte 等各类转换 http://www.cnblogs.com/warioland/archive/2012/03/06/2381355.html using (System.Net ...
- nginx基本运维及常用配置
nginx基本运维及常用配置 ========================================================== 基本运维 nginx 的启动 nginx -c /p ...
- 4、一个打了鸡血的for循环(增强型for循环)
对于循环,我们大家应该都不陌生,例如do-while循环,while循环,for循环,今天给大家介绍一个有趣的东西——打了鸡血的for循环(增强型for循环). 首先看代码,了解一下for循环的结构: ...
- kafka客户端和服务端开发(三)
前面我们已经搭建了kafka的单机和集群环境,分别写了简单的实例代码,对于代码里面使用到的参数并没有做解释.下面我们来详细说一下各个参数的作用. 1. 创建kafka生产者 kafka生产者有3个必选 ...
- idea+Spring+Mybatis+jersey+jetty构建一个简单的web项目
一.先使用idea创建一个maven项目. 二.引入jar包,修改pom.xml <dependencies> <dependency> <groupId>org. ...