立即加载还是延迟加载必须要连接数据库的,而在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的更多相关文章

  1. Hibernate4.0之HibernateSessionFactory源码详解

    import org.hibernate.HibernateException; import org.hibernate.Session; import org.hibernate.cfg.Conf ...

  2. Java EE之Hibernate的HibernateSessionFactory

    昨天,一下午都被一个bug缠身,最后逐层排查,发现是MyEclipse 2014自动生成的HibernateSessionFactory有问题.后观察网友提供的自动生成的HibernateSessio ...

  3. 如何理解Hibernate中的HibernateSessionFactory类

    package com.zz.util; import org.hibernate.HibernateException; import org.hibernate.Session; import o ...

  4. Struts2中的设计模式----ThreadLocal模式

    http://www.cnblogs.com/gw811/archive/2012/09/07/2675105.html 设计模式(Design pattern):是经过程序员反复实践后形成的一套代码 ...

  5. 【转】Struts2的线程安全 和Struts2中的设计模式----ThreadLocal模式

    [转]Struts2的线程安全 和Struts2中的设计模式----ThreadLocal模式 博客分类: 企业应用面临的问题 java并发编程 Struts2的线程安全ThreadLocal模式St ...

  6. ThreadLocal使用演示样例

    MainActivity例如以下: package cc.cv; import android.os.Bundle; import android.app.Activity; /** * Demo描写 ...

  7. Hibernate的数据查找,添加!

    1.首先看一下测试数据库的物理模型 2.测试所需要的Hibernate的jar包 3.数据库的sql /*=============================================== ...

  8. struts1&&Hibernate Demo1

    用struts1和Hibernate实现简单登录案例 主要思路:通过用户名name验证 如果一致则验证成功. 附上源码: 1.建立student表,这里使用的是mysql数据库,以下为该表的DDL: ...

  9. Hibernate3回顾-3-Session管理

    3.Session管理 仅为个人理解.请指正 3.1背景 由于Configuration的创建耗费系统的资源.所以有必要只将Configuration实例化一次,之后通过SessionFactory获 ...

随机推荐

  1. Reactjs-JQuery-Vuejs-Extjs-Angularjs对比

    写在前面 前端越来越混乱了,当然也可以美其名曰:繁荣. 当新启动一个前端项目,第一件事就是纠结:使用什么框架,重造什么轮子? 那么,希望看完此篇,能够给你一个清晰的认识,或者让你更加地纠结和无所适从 ...

  2. Android开发者的Kotlin:书

    原文标题:Kotlin for Android Developers: The book 原文链接:http://antonioleiva.com/kotlin-android-developers/ ...

  3. 提交本地项目到github服务器

    已经完成的本地项目 提交到github 并不是按照先在github上创建一个仓库 然后clone下来的顺序 1.在github上创建仓库 2.在本地项目初始化git仓库 $ git init 3.添加 ...

  4. CentOS 7 安装 Docker

    CentOS 7 安装 Docker 这里介绍 ContOS 7 的安装 docker V1.2+,包括阿里云加速 docker 镜像下载的设置,这对提升使用 docker 体验至关重要.其他系统安装 ...

  5. Reactor 模式的简单实现

    Reactor 模式简单实现 在网上有部分文章在描述Netty时,会提到Reactor.这个Reactor到底是什么呢?为了搞清楚Reactor到底是什么鬼,我写了一个简单的Demo,来帮助大家理解他 ...

  6. 转载 NPOI.dll 用法。单元格,样式,字体,颜色,行高,宽度。读写excel

    我用的版本是1.25的.每个版本用法有一点不同 using System; using System.Collections.Generic; using System.ComponentModel; ...

  7. Perforce 与Source Insight, Visual Studio集成

    转自:http://shashanzhao.com/archives/837.html 1.Perforce 首先需要为perforce设置系统环境变量,以便perforce命令行可以正常使用. 环境 ...

  8. 烂泥:python2.7和python3.5源码安装

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 前几天在centos6.6安装ansible时,一直提示python版本不对,导致不能安 ...

  9. [译]Thinking in React

    编者按 使用React的思想来构建应用对我在实际项目中以及帮助他人解决实际问题时起到了很大作用,所以我翻译此文来向那些正在或即将陷入React或React-Native深坑的同胞们表示慰问.网上已经有 ...

  10. node.js express 4.x 安装指南

    前几天express 推出了4.0,得知这个消息,自己尝试了一下,突然发现用以前的文档上的操作出现了各种问题.结果只能去看文档,现在在这个给大家分享下4.0版本的安装. 先说下如果需要用express ...