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 ...
随机推荐
- Full postback triggered by LinkButton inside GridView inside UpdatePanel
GridView inside of a UpdatePanel,get the button to trigger a partial postback <asp:ScriptManager ...
- golang学习之regexp
regexp是golang标准库自带的正则校验包,使用: re, _ := regexp.Compile(`(\d+)年(\d+)月`) //判断是否匹配category类别搜索 ismatch := ...
- GC回收策略
判读一个对象是否“已死”: 引用计数算法:给对象中添加一个引用计数器,每当有一个地方引用它时,计数器值就加1:当引用失效时,计数器值就减1:任何时刻计数器为0的对象就是不可能再被使用的. 至少主流的J ...
- my docker note
环境: docker1.10.3 #hello docker docker run --name myhello docker.io/centos:67591570dd29 /bin/echo 'he ...
- 初识http2.0
初识http2.0 HTTP2.0性能增强的核心:二进制分帧 HTTP 2.0最大的特点: 不会改动HTTP 的语义,HTTP 方法.状态码.URI 及首部字段,等等这些核心概念上一如往常,却能致力于 ...
- JavaScript之如何对客户端进行检测
本文主要是针对各种客户端进行检测,使用了用户代理字符串检测技术,具体代码如下: var client=function() { var engine= { // 呈现引擎 ie: 0, gecko: ...
- 基于easyUI实现权限管理系统(一)一—组织结构树图形
此文章是基于 EasyUI+Knockout实现经典表单的查看.编辑 一. 相关文件介绍 1. organize.jsp:组织结构树的主界面 <!DOCTYPE html PUBLIC &quo ...
- svn 创建本地仓库
1. svnadmin create ~/repository 2. svnserve -d -r ~/repository 3. svn checkout file://~/repository $ ...
- JAVA SwingWorkder的使用例
最近在学习Swing,我们都知道在UI表现线程里面长时间执行操作时,画面会假死,为了能够让费时操作不影响画面表现,就需要用多线程了.首先考虑的就是Swing内部的 SwingWorkder对象,但是网 ...
- Effective C++ .33 子类的名称覆盖
#include <iostream> #include <cstdlib> using namespace std; class Base { public: int add ...