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 ...
随机推荐
- 初探Oracle全栈虚拟机---GraalVM
官方说明: GraalVM是一个生态系统和共享运行时,不仅提供基于JVM的语言(如Java,Scala,Groovy和Kotlin)的性能优势,还提供其他编程语言(如JavaScript,Ruby,P ...
- 重复造轮子系列——基于FastReport设计打印模板实现桌面端WPF套打和商超POS高度自适应小票打印
重复造轮子系列——基于FastReport设计打印模板实现桌面端WPF套打和商超POS高度自适应小票打印 一.引言 桌面端系统经常需要对接各种硬件设备,比如扫描器.读卡器.打印机等. 这里介绍下桌面端 ...
- Linux命令(部分)
LINUX:实现某一功能,命令执行依赖于解释器程序. 内部:属于shell部分 外部:独立于shell解释器程序. 系统结构由外到内:用户 ⇢ 外围程序 ⇢ 硬件 ...
- 【Java】设置 JPanel 宽度
panel.setSize(200, 300); //该方法无效 panel.setPreferredSize(new Dimension(800, 0)); //使用该方法 参考链接: http:/ ...
- JSON在线格式化 jsoneditor使用
const placeholder = { string: 'hello world!', boolean: true, color: '#6c928c', number: 123, null: nu ...
- Educational Codeforces Round 70 (Rated for Div. 2)
这次真的好难...... 我这个绿名蒟蒻真的要崩溃了555... 我第二题就不会写...... 暴力搜索MLE得飞起. 好像用到最短路?然而我并没有学过,看来这个知识点又要学. 后面的题目赛中都没看, ...
- Android使用xUtils3上传图片报错解决:java.lang.ArrayIndexOutOfBoundsException: 70918
今天在使用安卓xUtils3框架配合SmartUpload框架上传图片到Java服务端时,遇到了一个莫名其妙的错误: 安卓端代码如下: 似乎并没有发现什么问题,以前在用xUtils2.6老版本时也是这 ...
- Zookeeper开源客户端Curator的使用
开源zk客户端-Curator 创建会话: RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000,3); CuratorFramewor ...
- android ——悬浮按钮及可交互提示
一.悬浮按钮 FloatingActionButton是Design Support中的一个控件,它会默认colorAccent作为按钮的颜色,还可以给按钮一个图标. 这是没有图标的,这是有图标的. ...
- mui的app页面使用layui填充数据
在mui的开发中有个坑,mui.plusReady在web上使用时是不会起作用的,只能在app上才行,所以推荐自己测试时使用mui.ready去写加载时的方法. 前端请求的返回格式为json,所以在后 ...