来源(转载):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的理解

线程局部变量,就是为每一个使用某变量的线程都提供一个该变量值的副本,是每一个线程都可以独立的改变自己的副本,而不会和其他线程的副本冲突,从线程的

角度看就好像每一个线程都完全拥有一个变量

实例代码:

  1. package com.capinfotech.ju.util;
  2. import org.hibernate.HibernateException;
  3. import org.hibernate.Session;
  4. import org.hibernate.SessionFactory;
  5. import org.hibernate.cfg.Configuration;
  6. public class HibernateUtil {
  7. public static final ThreadLocal sessionThreadLoacl = new ThreadLocal();
  8. public static final SessionFactory sessionFactory;
  9. static {
  10. try {
  11. sessionFactory = new Configuration().configure().buildSessionFactory();
  12. } catch(Throwable t) {
  13. throw new ExceptionInInitializerError(t);
  14. }
  15. }
  16. /**
  17. * 获取当前的Session
  18. * @return
  19. * @throws HibernateException
  20. */
  21. public static Session currentSession() throws HibernateException {
  22. Session session = (Session) sessionThreadLoacl.get();
  23. if(session == null) {
  24. session = sessionFactory.openSession();
  25. sessionThreadLoacl.set(session);
  26. }
  27. return session;
  28. }
  29. /**
  30. * 释放Session
  31. * @throws HibernateException
  32. */
  33. public static void closeSession() throws HibernateException {
  34. Session session = (Session)sessionThreadLoacl.get();
  35. if(session != null) {
  36. session.close();
  37. }
  38. sessionThreadLoacl.set(null);
  39. }
  40. }
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的理解的更多相关文章

  1. HIbernate中openSession和getCurrentSession

      这两者的差别网上非常多资源,我这里就copy一下了,然后有点问题的是今天遇到的问题.   openSession和getCurrentSession的根本差别在于有没有绑定当前线程,所以,用法有差 ...

  2. hibernate中openSession()跟getCurrentSession()方法之间的区别

    Hibernate openSession() 和 getCurrentSession的区别 getHiberanteTemplate .getCurrentSession和OpenSession 采 ...

  3. Hibernate中openSession() 与 getCurrentSession()的区别

    1 getCurrentSession创建的session会和绑定到当前线程,而openSession每次创建新的session. 2 getCurrentSession创建的线程会在事务回滚或事物提 ...

  4. Hibernate之openSession与getCurrentSession的区别

    openSession 与 getCurrentSession的区别(1)openSession 每一次获得的是一个全新的session对象,而getCurrentSession获得的是与当前线程绑定 ...

  5. hibernate 的SessionFactory的getCurrentSession 与 openSession() 的区别

    1 getCurrentSession创建的session会和绑定到当前线程,而openSession不会. 2 getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而ope ...

  6. openSession和getCurrentSession的比较

    在比较openSession和getCurrentSession这两个方法之前,我们先认识一下这两个方法. 在进行配置信息管理时,我们一般进行一下简单步骤: Configuration cfg = n ...

  7. sessionFactory中的openSession和getCurrentSession的一些注意事项

    今天进行Hibernate测试时遇到了一个问题 我在用sessionFactory生产seesion时出现了故障,使用getCurrentsesstion时产生异常: Exception in thr ...

  8. openSession()与getCurrentSession()的区别

    getCurrentSession创建的session会和绑定到当前线程,而openSession不会. getCurrentSession创建的线程会在事务回滚或事物提交后自动关闭,而openSes ...

  9. hibernate3.6-联合主键注解以及openSession和getCurrentSession区别

    [联合主键]>>>>配置方式:xml:    1. Student中单独创建StudentPk主键实体类 2. 配置: <composite-id name=" ...

随机推荐

  1. android app 集成 信鸽推送

    推送其实挺中意小米推送的,并经用户群占比还是比较大的,奈何拗不过php后端哥们的选型,就只好用信鸽推送了,期间接入过程中也是遇到不少问题,所以记录下来,以后如果还是用信鸽推送的话,估计看看以前的博客, ...

  2. 设计模式--命令模式(Command)

    基本概念:  Command模式也叫命令模式 ,是行为设计模式的一种.Command模式通过被称为Command的类封装了对目标对象的调用行为以及调用参数,命令模式将方法调用给封装起来了. 命令模式的 ...

  3. 《JS权威指南学习总结--第五章语句》

    内容要点: 一.throw语句     所谓异常是当发生了某种异常情况或错误时产生的一个信号.     抛出异常就是用信号通知发生了错误或异常状况.     捕获异常时指处理这个信号,即采取必要的手段 ...

  4. POJ 1860 Currency Exchange(SPFA+邻接矩阵)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<cmath> #include<algori ...

  5. hdu 2993 MAX Average Problem(斜率DP入门题)

    题目链接:hdu 2993 MAX Average Problem 题意: 给一个长度为 n 的序列,找出长度 >= k 的平均值最大的连续子序列. 题解: 这题是论文的原题,请参照2004集训 ...

  6. php和js的转成整数的方法

    1.将变量转成整数类型php: intval(7/2)js:parseInt(7/2)2.向上取整,有小数就整数部分加1php: ceil(7/2)js: Math.ceil(7/2)3,四舍五入.p ...

  7. SQLServer乱码问题的分析及解决方法(中文字符被存入数据库后,显示为乱码)

    注:本文为个人转存,原文地址:http://blog.csdn.net/qiuyu8888/article/details/8021410 问题:SQL版在使用过程中有时会出现乱码,我的症状是中文字符 ...

  8. C#正则分组实例

    static void Main(string[] args) { string str = "大家家家家家家家明天天天天天天天天玩得得得得得得得开心"; Regex reg = ...

  9. hdu 5874 Friends and Enemies icpc大连站网络赛 1007 数学

    #include<stdio.h> #include<iostream> #include<algorithm> #include<math.h> #i ...

  10. 评测:VPS推荐digitalocean和Vultr和Linode

    美国vps推荐三家,分别是digitalocean.vultr和linode,拥有很高的性价比,中国访问速度快.我是上面三家的早期用户,并且一直使用至今,积累了不少使用经验. DigitalOcean ...