Hibernate关于openSession和getCurrentSession的理解
来源(转载):http://blog.csdn.net/woshisap/article/details/7024482
1:getCurrentSession会把Session和当前的线程关联起来,而openSession只是重新开启一个Session
2:getCurrentSession获得的Session会在事务关闭或者回滚时会自动关闭,而openSession获得的Session必须手动关闭
getCurrentSession,特定的实现用它来负责跟踪当前的上下文session,Hibernate内置了此接口的两种实现
* org.hibernate.context.JTASessionContext --当前session通过当前执行的线程来跟踪和界定
* org.hibernate.context.ThreadLocalSessionContext --当前线程通过当前执行的线程和跟踪和界定
这两种实现都提供了“每数据库事务对应一个session”的编程模型,也称作每次请求一个session,Hibernate session的起始和终结由数据库事务的生存来控制。
3:关于ThreadLocal的理解
线程局部变量,就是为每一个使用某变量的线程都提供一个该变量值的副本,是每一个线程都可以独立的改变自己的副本,而不会和其他线程的副本冲突,从线程的
角度看就好像每一个线程都完全拥有一个变量
实例代码:
- package com.capinfotech.ju.util;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class HibernateUtil {
- public static final ThreadLocal sessionThreadLoacl = new ThreadLocal();
- public static final SessionFactory sessionFactory;
- static {
- try {
- sessionFactory = new Configuration().configure().buildSessionFactory();
- } catch(Throwable t) {
- throw new ExceptionInInitializerError(t);
- }
- }
- /**
- * 获取当前的Session
- * @return
- * @throws HibernateException
- */
- public static Session currentSession() throws HibernateException {
- Session session = (Session) sessionThreadLoacl.get();
- if(session == null) {
- session = sessionFactory.openSession();
- sessionThreadLoacl.set(session);
- }
- return session;
- }
- /**
- * 释放Session
- * @throws HibernateException
- */
- public static void closeSession() throws HibernateException {
- Session session = (Session)sessionThreadLoacl.get();
- if(session != null) {
- session.close();
- }
- sessionThreadLoacl.set(null);
- }
- }
- package com.capinfotech.ju.util;
- import org.hibernate.HibernateException;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.cfg.Configuration;
- public class HibernateUtil {
- public static final ThreadLocal sessionThreadLoacl = new ThreadLocal();
- public static final SessionFactory sessionFactory;
- static {
- try {
- sessionFactory = new Configuration().configure().buildSessionFactory();
- } catch(Throwable t) {
- throw new ExceptionInInitializerError(t);
- }
- }
- /**
- * 获取当前的Session
- * @return
- * @throws HibernateException
- */
- public static Session currentSession() throws HibernateException {
- Session session = (Session) sessionThreadLoacl.get();
- if(session == null) {
- session = sessionFactory.openSession();
- sessionThreadLoacl.set(session);
- }
- return session;
- }
- /**
- * 释放Session
- * @throws HibernateException
- */
- public static void closeSession() throws HibernateException {
- Session session = (Session)sessionThreadLoacl.get();
- if(session != null) {
- session.close();
- }
- sessionThreadLoacl.set(null);
- }
- }
在sessionFactory启动的时候,Hibernate会根据配置创建相应的CurrentSessionContext,在getCurrentSession()被调用的时候,实际被执行的方法是CurrentSessionContext.currentSession(),在currentSession()执行时,如果当前Session为空,currentSession会调用SessionFactory的openSession,
这样既保证了Session的线程安全,又不用每次数据库操作都重新获取一个Session实例,实现了Session重用
Hibernate关于openSession和getCurrentSession的理解的更多相关文章
- HIbernate中openSession和getCurrentSession
这两者的差别网上非常多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题. openSession和getCurrentSession的根本差别在于有没有绑定当前线程,所以,用法有差 ...
- hibernate中openSession()跟getCurrentSession()方法之间的区别
Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...
- Hibernate中openSession() 与 getCurrentSession()的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...
- Hibernate之openSession与getCurrentSession的区别
openSession 与 getCurrentSession的区别(1)openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定 ...
- hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别
1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...
- openSession和getCurrentSession的比较
在比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法. 在进行配置信息管理时,我们一般进行一下简单步骤: Configuration cfg = n ...
- sessionFactory中的openSession和getCurrentSession的一些注意事项
今天进行Hibernate测试时遇到了一个问题 我在用sessionFactory生产seesion时出现了故障,使用getCurrentsesstion时产生异常: Exception in thr ...
- openSession()与getCurrentSession()的区别
getCurrentSession创建的session会和绑定到当前线程,而openSession不会. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSes ...
- hibernate3.6-联合主键注解以及openSession和getCurrentSession区别
[联合主键]>>>>配置方式:xml: 1. Student中单独创建StudentPk主键实体类 2. 配置: <composite-id name=" ...
随机推荐
- ios隐藏软键盘
//判断是否为苹果 var isIPHONE = navigator.userAgent.toUpperCase().indexOf('IPHONE')!= -1; // 元素失去焦点隐藏iphone ...
- Java中的使用了未经检查或不安全的操作
1. javac -Xlint:unchecked MyMath.java检查不安全的操作 /***************************************************/ ...
- 2016WHD.china世界云计算日·北京站即将召开
WHD.china自进驻中国以来,已在上海.北京成功举办多届,2015年于北京举办的会议更是盛况空前,注册参会者逾800人次,汇聚了国内外众多知名云服务商.IDC商.电子商务企业.电信运营商.ISP等 ...
- doc 窗口操作图
doc 窗口操作图doc 窗口操作图vdoc 窗口操作图
- C# Monads的实现(二)
再谈continuation monad 上一篇中我们已经介绍了continuation monad,但是这个monad与Identity,Maybe,IEnumerable monads稍微难于理解 ...
- ESFramework 4.0 进阶(01)-- 消息
需要交互的分布式系统之间通过消息来传递有意义的信息.消息是通信框架的核心.离开了消息,再谈通信框架就没有任何意义,所以,消息是ESFramework中一个最核心的概念. 一. 消息的类别 在具体的应用 ...
- RFC3489 STUN之客户端所处环境探测流程与部分属性含义说明
1 STUN客户端所处环境探测流程 1.1 流程图 1.2 流程图中Binding请求类型说明 类型1:Binding请求消息中不设置CHANGE-REQUEST,或若设置其相应更改IP与端口标志位都 ...
- stm32菜单按键的设计
有点懒.看注释吧 // k0,enter/enable;k3:esc/disable// k1,value+/menu+;k2:menu-/value-; #include "sysmenu ...
- JPA 系列教程16-继承-联合子类-JOINED
联合子类策略 这种情况下子类的字段被映射到各自的表中,这些字段包括父类中的字段,并执行一个join操作来实例化子类. 举例 如果实体类Teacher继承实体类Person,实体类Student也继承自 ...
- postgresql 修改属性
up vote2down votefavorite From this article, I tried to update or delete property of a JSONB column: ...