HibernateSessionFactory建立-使用ThreadLocal
立即加载还是延迟加载必须要连接数据库的,而在Java中连接数据库是依赖java.sql.Connection,在hibernate中session就是Connection的一层高级封装,一个session对应了一个Connection,要实现延迟加载必须有session才行.而且要进行延迟加载还必须保证是同一个session才行,用另外一个session去延迟加载前一个session的代理对象是不行的.大家都知道Connection是使用过后必须要进行关闭的,那么我们如何保证一次http请求过程中,一直都使用一个session呢,即一个Connection呢.而且还要保证http请求结束后正确的关闭.
好,现在我们知道了我们要解决的问题
1.如何保证http请求结束后正确的关闭session
2.如何保证http请求过程中一直使用同一个session
package util; import java.io.Serializable; import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration; public class HibernateSessionFactory implements Serializable{
private static final String CONFIG_FILE_LOCATION = "/hibernate.cfg.xml";
private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
private static Configuration configuration = new Configuration();
private static SessionFactory sessionFactory=null;
private static String configFile = CONFIG_FILE_LOCATION;
//static代码块只会在实例化类的时候只执行一次
static{
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
} }
//获取Session
public static Session getCurrentSession(){
Session session = threadLocal.get();
//判断Session是否为空,如果为空,将创建一个session,并付给线程变量tLocalsess
try {
if(session ==null&&!session.isOpen()){
if(sessionFactory==null){
rbuildSessionFactory();
}else{
session = sessionFactory.openSession();
}
//session = (sessionFactory != null) ? sessionFactory.openSession(): null;
}
threadLocal.set(session);
} catch (Exception e) {
// TODO: handle exception
} return session;
} public static void rbuildSessionFactory(){
try {
configuration.configure(configFile);
sessionFactory = configuration.buildSessionFactory();
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
}
HibernateSessionFactory建立-使用ThreadLocal的更多相关文章
- Hibernate4.0之HibernateSessionFactory源码详解
import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Conf ...
- Java EE之Hibernate的HibernateSessionFactory
昨天,一下午都被一个bug缠身,最后逐层排查,发现是MyEclipse 2014自动生成的HibernateSessionFactory有问题.后观察网友提供的自动生成的HibernateSessio ...
- 如何理解Hibernate中的HibernateSessionFactory类
package com.zz.util; import org.hibernate.HibernateException; import org.hibernate.Session; import o ...
- Struts2中的设计模式----ThreadLocal模式
http://www.cnblogs.com/gw811/archive/2012/09/07/2675105.html 设计模式(Design pattern):是经过程序员反复实践后形成的一套代码 ...
- 【转】Struts2的线程安全 和Struts2中的设计模式----ThreadLocal模式
[转]Struts2的线程安全 和Struts2中的设计模式----ThreadLocal模式 博客分类: 企业应用面临的问题 java并发编程 Struts2的线程安全ThreadLocal模式St ...
- ThreadLocal使用演示样例
MainActivity例如以下: package cc.cv; import android.os.Bundle; import android.app.Activity; /** * Demo描写 ...
- Hibernate的数据查找,添加!
1.首先看一下测试数据库的物理模型 2.测试所需要的Hibernate的jar包 3.数据库的sql /*=============================================== ...
- struts1&&Hibernate Demo1
用struts1和Hibernate实现简单登录案例 主要思路:通过用户名name验证 如果一致则验证成功. 附上源码: 1.建立student表,这里使用的是mysql数据库,以下为该表的DDL: ...
- Hibernate3回顾-3-Session管理
3.Session管理 仅为个人理解.请指正 3.1背景 由于Configuration的创建耗费系统的资源.所以有必要只将Configuration实例化一次,之后通过SessionFactory获 ...
随机推荐
- ajax总结:
1.原生ajax get请求和post请求区别:黄色小三角 以get请求为例,输出结果如下: 2.jquery中的ajax 列了常用的6个方法: 3.状态说明 readystate: 0:请求未初始化 ...
- 使用PullToRefresh插件实现ListView下拉刷新(Android Studio)
下载PullToRefresh
- Activity的释放
1.ActivityTwo.finish(); 在你的activity动作完成的时候,或者Activity需要关闭的时候,调用此方法.当你调用此方法的时候,系统只是将最上面的Activity移出了栈, ...
- chrome浏览器限制的端口
1, // tcpmux 7, // echo 9, // discard 11, // systat 13, // daytime 15, // netstat 17, ...
- C#接口的显示和隐式实现
早上骑车上班走到半路发现手机忘带了,这年代兜里没装一分现金,吃饭都要刷手机,上班时间无聊了不能玩手机更是大问题,所以果断掉头拿手机.取完手机刚出门看这天阴沉沉的貌似要下雨,没雨衣,骑车又不能打伞,上次 ...
- [Erlang 0126] 我们读过的Erlang论文
我在Erlang Resources 豆瓣小站上发起了一个征集活动 [链接] ,"[征集] 我们读过的Erlang论文",希望大家来参加.发起这样一个活动的目的是因为Erlang相 ...
- PHP中的数据库二、memcache
:first-child, ol li > :first-child, ul li ul:first-of-type, ol li ol:first-of-type, ul li ol:firs ...
- 二进制包安装MySQL数据库
1.1二进制包安装MySQL数据库 1.1.1 安装前准备(规范) [root@Mysql_server ~]# mkdir -p /home/zhurui/tools ##创建指定工具包存放路径 [ ...
- Morris post order traversal algorithm
Sept. 5, 2015 花时间把代码读明白, 比光看书强. 动手写代码, 改代码, 兴趣是最好的老师. 多记几个例子, 增加情趣. 举个例子关于中序遍历, 4 ...
- 基于C/S架构的3D对战网络游戏C++框架_05搭建系统开发环境与Boost智能指针、内存池初步了解
本系列博客主要是以对战游戏为背景介绍3D对战网络游戏常用的开发技术以及C++高级编程技巧,有了这些知识,就可以开发出中小型游戏项目或3D工业仿真项目. 笔者将分为以下三个部分向大家介绍(每日更新): ...