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=" ...
随机推荐
- Linux使用rsync客户端与服务端同步目录进行备份
一.服务端设置 1. 修改 server 端配置 # vi /etc/rsyncd.conf 修改: uid = nobody # 该选项指定当该模块传输文件时守护进程应该具有的uid.默认值为&qu ...
- HDU 2181 哈密顿绕行世界问题
Problem Description 一个规则的实心十二面体,它的 20个顶点标出世界著名的20个城市,你从一个城市出发经过每个城市刚好一次后回到出发的城市. Input 前20行的第i行有3个数, ...
- jQuery获取元素的兄弟节点的几种方法
$('#id').siblings() //当前元素所有的兄弟节点 $('#id').prev() //当前元素前一个兄弟节点 $('#id').prevaAll() //当前元素之前所有的兄弟节点 ...
- C语言:全局变量在多个c文件中公用的方法 [转]
用C语言编写程序的时候,我们经常会遇到这样一种情况:希望在头文件中定义一个全局变量,然后包含到两个不同的c文件中,希望这个全局变量能在两个文件中共用. 举例说明:项目文件夹project下有main. ...
- Java 反射 调用私有域和方法(setAccessible)
Java 反射 调用私有域和方法(setAccessible) @author ixenos AccessibleObject类 Method.Field和Constructor类共同继承了Acces ...
- MySQL(3)-索引
一.索引类型 在MySQL中,存储引擎使用索引,首先在索引中找到对应值,然后根据匹配的索引记录中找到对应的行. 无论是多么复杂的ORM工具,在精妙和复杂的索引面前都是"浮云".这里 ...
- 深入理解Java内存模型 - volatile
volatile的特性 当我们声明共享变量为volatile后,对这个变量的读/写将会很特别.理解volatile特性的一个好方法是:把对volatile变量的单个读/写,看成是使用同一个监视器锁对这 ...
- .Net Core Session验证码
1.验证码帮助类 namespace IdeaCore.Services.Common { public class ValidateCodeService : IValidateCodeServic ...
- Openjudge-计算概论(A)-完美立方
描述: a的立方 = b的立方 + c的立方 + d的立方为完美立方等式.例如12的立方 = 6的立方 + 8的立方 + 10的立方 .编写一个程序,对任给的正整数N (N≤100),寻找所有的四元组 ...
- socket通信实例
ref: http://www.cnblogs.com/xudong-bupt/archive/2013/12/29/3483059.html http://blog.csdn.net/love_ga ...