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获 ...
随机推荐
- 【原】Bootstrap+Knockout.JS+ASP.Net MVC3+PetaPOCO实现CRUD操作
1.需求: 1.1)页面要美观大气 1.2)前端代码要简洁清晰,要用MVC或是MVVM框架 1.3)服务端要用MVC框架,要Rest风格 1.4)数据访问要用ORM 2.效果: 2.1)列表 2.2) ...
- iOS 汉字的拼音
获取汉字的拼音 #import <Foundation/Foundation.h> @interface NSString (Utils) /** * 汉字的拼音 * * @return ...
- TileJSON
TileJSON TileJSON is an open standard for representing map metadata. License The text of this specif ...
- React-Native坑:Invariant Violation:Application 项目名 has not been registered.
前言 在学习一门新技术的你也许有跟我一样的困惑,照着书上或者视频上的敲了.但是就是有各种问题没有出来自己想要的结果.我会将自己在这个过程中遇到的坑都记录下来,不一定全覆盖,但希望这些文章可以解决你的问 ...
- Cocos2dx中线程优先级
Cocos2dx中线程优先级问题 不论是ios还是android,遇到耗时的任务都要另起线程处理,否则程序不能及时用户的反馈.游戏中如果一圈循环不能在1/frameRate(帧率是30则1/30)秒内 ...
- Curator框架的使用
Curator框架的目的是减少用户的复杂度,毕竟原生的Zookeeper难以使用. 这里举一个使用例子. 第一步:建立连接 // 以下代码与192.168.1.101:2181建立了连接Curator ...
- [Erlang 0126] 我们读过的Erlang论文
我在Erlang Resources 豆瓣小站上发起了一个征集活动 [链接] ,"[征集] 我们读过的Erlang论文",希望大家来参加.发起这样一个活动的目的是因为Erlang相 ...
- [Erlang 0121] 当我们谈论Erlang Maps时,我们谈论什么 Part 3
Erlang/OTP 17.0 has been released http://www.erlang.org/download/otp_src_17.0.readme Erlang/OTP ...
- CSS3:radial-gradient,径向渐变的使用方法
语法 径向渐变不同于线性渐变,线性渐变是从“一个方向”向“另一个方向”的颜色渐变,而径向渐变是从“一个点”向四周的颜色渐变.其语法如下: background: radial-gradient(cen ...
- resize2fs命令使用
如果我们创建一个loop设备然后将其挂载后,但是还要加大空间时,可以使用resize2fs命令来增大. 首先创建一个loop设备 dd 之后做成loop设备 losetup /dev/loop0 a. ...