Hibernate关于Session管理的openSession和getCurrentSession的理解
来源:互联网 发布:什么叫农村淘宝 编辑:程序博客网 时间:2024/05/16 12:45
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);}}
在sessionFactory启动的时候,Hibernate会根据配置创建相应的CurrentSessionContext,在getCurrentSession()被调用的时候,实际被执行的方法是CurrentSessionContext.currentSession(),在currentSession()执行时,如果当前Session为空,currentSession会调用SessionFactory的openSession,这样既保证了Session的线程安全,又不用每次数据库操作都重新获取一个Session实例,实现了Session重用。
- Hibernate关于Session管理的openSession和getCurrentSession的理解
- Hibernate关于Session管理的openSession和getCurrentSession的理解
- hibernate的session判断-openSession和getCurrentSession
- Hibernate两种获取session的方式openSession和getCurrentSession的区别与理解
- 关于spring+hibernate中的OpenSession()和getCurrentSession()的区别
- hibernate 的openSession和getCurrentSession的区别
- hibernate 的 openSession 和 getCurrentSession 的实现
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate openSession() 和 getCurrentSession的区别
- Hibernate中openSession 和 getCurrentSession 的区别
- Hibernate 中 openSession()和getCurrentSession() 的区别
- Hibernate openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate之openSession()和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别
- hibernate: openSession() 和getCurrentSession()的区别
- Hibernate openSession() 和 getCurrentSession的区别 .
- Hibernate的openSession和getCurrentSession区别
- jquery radio取值,checkbox取值,select取值,radio选中,checkbox选中,select选中,及其相关
- php safe_mode
- 斐波那契数列的递归优化实现
- XML读写中遇到的错误整理
- web安全(XSS&CSRF)
- Hibernate关于Session管理的openSession和getCurrentSession的理解
- 关闭fedora18 防火墙
- 二叉树 Poj 1577 Falling Leaves
- mysql 时间操作大全 注意本文从网上收集运算符坑得很!!!
- Java抽象类和接口的区别
- 16进制字符串转字节数组
- Java笔记(使用Eclipse) 边走边聊
- 基于Java的四大开源测试工具
- WPF Page和UserControl的区别