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=" ...
随机推荐
- sublime text3编译运行C,Java程序的一些配置
环境:linux 64位 桌面环境: gnome Java编译运行 (1)Preferences --> Browse Packages --> 在该文件夹下新建build文件如: Myj ...
- html5权威指南:标记文字、组织内容、文档分节
HTML5新增及删除标签:http://www.cnblogs.com/starof/archive/2015/06/23/4581850.html 第八章:标记文字 ...
- 处理html页面元素工具类(HtmlAgilityPack.dll)的使用
下载地址:http://htmlagilitypack.codeplex.com/ 1.添加HtmlAgilityPack.dll引用(引用类using HtmlAgilityPack;). 2.简单 ...
- ajax格式,需要指定交互的data类型
思路 先打印所有返回值 console.log(d); {''status'':999} 再查看返回值,能否转换为json dataType:'json', //大小写敏感. datatype ...
- AUTO_INCREMENT列在InnoDB里如何工作
如果你为一个表指定AUTO_INCREMENT列,在数据词典里的InnoDB表句柄包含一个名为自动增长计数器的计数器,它被用在为该列赋新值.自动增长计数器仅被存储在主内存中,而不是存在磁盘上. Inn ...
- 对一个表中所有列数据模糊查询adoquery
如何用adoquery对一个表中所有列进行模糊查询: procedure TForm3.Button4Click(Sender: TObject); var ASql,AKey: string; I: ...
- fatal error RC1004: unexpected end of file found处理方法
资源编译器错误 RC1004 错误消息 遇到意外的文件结束 此错误是由于文本文件的最后一行中缺少换行符和回车符而造成的.
- 高精度运算专题3-乘法运算(The multiplication operation)
这个专题呢,我就来讲讲高精度的乘法,下面是三个计算乘法的函数,第一个函数是char类型的,要对字符串进行数字转换,而第二个是两个int类型的数组,不用转换成数字,第三个则更为优化,用a数组-b数组放回 ...
- crontab定时任务
使用cron服务,用 service crond status 查看 cron服务状态,如果没有启动则 service crond start启动它, cron服务是一个定时执行的服务,可以通过cro ...
- Git Bash 简单操作
在Windows下使用Git Bash,用的是Linux命令,常用几个文件操作命令如下: Windows命令 Linux命令 意义 cd e:\xxx cd /e/xxx 切换到xxx目录 cd pw ...