Hibernate之openSession与getCurrentSession的区别
openSession 与 getCurrentSession的区别
(1)openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定的session对象;
(2)openSession不需要配置,而getCurrentSession需要配置
<property name="current_session_context_class">thread</property>
(3)openSession需要手动关闭,而getCurrentSession系统自动关闭
openSession出来的session要通过:session.close(), 而getSessionCurrent出来的session系统自动关闭,如果自己关闭会报错
(4)Session是线程不同步的,要保证线程安全就要使用getCurrentSession
下面这段代码运行后可比较它们的(1)
package cn.blog.test; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class Test { //openSession与getCurrentSession对比 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()); //每次创建都是新的session对象
System.out.println(sessionThread1.hashCode() + "<-------->" + sessionThread2.hashCode()); //每次获得的是当前session } }
Hibernate之openSession与getCurrentSession的区别的更多相关文章
- Hibernate中openSession() 与 getCurrentSession()的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...
- hibernate中openSession()跟getCurrentSession()方法之间的区别
Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...
- openSession()与getCurrentSession()的区别
getCurrentSession创建的session会和绑定到当前线程,而openSession不会. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSes ...
- HIbernate中openSession和getCurrentSession
这两者的差别网上非常多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题. openSession和getCurrentSession的根本差别在于有没有绑定当前线程,所以,用法有差 ...
- Hibernate关于openSession和getCurrentSession的理解
来源(转载):http://blog.csdn.net/woshisap/article/details/7024482 1:getCurrentSession会把Session和当前的线程关联起来, ...
- openSession 与 getCurrentSession的区别
1.openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定的session对象 package cn.kiwifly.view; ...
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...
- hibernate3.6-联合主键注解以及openSession和getCurrentSession区别
[联合主键]>>>>配置方式:xml: 1. Student中单独创建StudentPk主键实体类 2. 配置: <composite-id name=" ...
- openSession和getCurrentSession的比较
在比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法. 在进行配置信息管理时,我们一般进行一下简单步骤: Configuration cfg = n ...
随机推荐
- IIS网站不能访问
摘要:IIS环境下,部署的网站在服务器上可以正常访问,客户端却不能访问. 原因:防火墙入站规则万维网服务没有开启.
- Java 基础(8)——流程控制
上次的运算符都消化好了吗?每一天都要用到一些哦~ 以前有提到过一嘴,程序执行都是从上到下执行的,emm,学到这里,感觉这句话是对的也是错的了…… 如果都是一行一行执行下去的话,上节课的例子: 今天不上 ...
- 一些在线开发手册api文档收藏
java JavaSE8 api:https://docs.oracle.com/javase/8/docs/api/ JavaSE7 api:http://docs.oracle.com/javas ...
- 监听域对象创建和销毁的Listener
1.什么是Servlet监听器? 先来看看什么是监听器.监听器是专门用于对其它对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监视的对象发生情况时立即采取相应的行动.Servlet监听器是S ...
- opencv 从摄像头中读取视频并保存(c++版)
原文:http://blog.csdn.net/zhongshijunacm/article/details/68947890 OpenCV中的视频操作函数如下表所列: VideoCapture Vi ...
- javascript面向对象的写法03
javascript面向对象的写法03 js一些基础知识的说明 prototype 首先每个js函数(类)都有一个prototype的属性,函数是类.注意类有prototype,而普通对象没有. js ...
- 如何为运行的 ARM Linux 启用 LAD2.3 版本的诊断扩展
Linux Azure Diagnostic (LAD) 扩展现在已经发布了 3.0 版本,但在 Azure 中国区,目前可用的最新版本还是 2.3. 虽然 Azure 门户提供了简单的操作版本为 L ...
- 批量检查多个网址是否正常(shell编程)
#!/bin/bash #version 1.1 #by zengj #using checking urls . /etc/init.d/functions checkurl() { wget -o ...
- Struts2学习-struts+spring
学习帮助网址:http://www.cnblogs.com/S-E-P/archive/2012/01/18/2325253.html http://blog.csdn.net/ad921012/ar ...
- June 19th 2017 Week 25th Monday
Everyone is dissatisfied with his own fortune. 人对自己的命运总是感到不满足. We always want more, even when we hav ...