Java Hibernate使用SessionFactory创建Session案例详解
作者:xueliangfei 时间:2022-03-04 06:21:28
SessionFactory在Hibernate中实际上起到了一个缓冲区的作用 他缓冲了HIbernate自动生成SQL语句和其他的映射数据 还缓冲了一些将来有可能重复利用的数据
为了能创建一个SessionFactory对象 应该在Hibernate初始化的时候创建一个Configuration类的实例 并将已经写好的映射文件交给他处理 这样Configuration对象就可以创建一个SessionFactory对象 当SessionFactory对象创建成功后 Configuration对象就没用用了 就可以简单的抛弃他
示例代码:
Configuration cfg = new Configuration();
cfg.addResource("com/demo/hibernate/beans/User.hbm.xml");
cfg.setProperty(System.getProperties());
SessionFactory sessionFactory = cfg.buildSessionFactory();
SessionFactory用到了一个设计模式 工厂模式 用户程序从工程类SessionFactory取得Session实例 设计者的意图就是让它能在整个应用中共享 典型的来说 一个项目通常只需要一个SessionFactory就够了 因此我们就设计了HibernateSessionFactory.java这个辅助类 定义了一个静态的Configuration和SessionFactory对象
private static final Configuration cfg = new Configuration();
private static org.hibernate.SessionFactory sessionFactory;
这两个对象对整个应用来说只有一个实例存在 因此为用户的访问定义一个本地线程变量:
private static final ThreadLocal threadLocal = new ThreadLocal();
该线程变量是静态的 对每一个访问该线程的用户产生一个实例 这样在要取得Session对象时 首先从当前用户的线程中取得Session对象 如果还没有创建 则从SessionFactory中创建一个Session 此时会判断SessionFactory对象是否已经创建 该对象对这个应用来说 只有一个 因此 只有第一次访问该变量的用户才会创建该对象
HibernateSessionFactory.java 取得Session对象的过程如下表示
public static Session currentSession() throws HibernateException {
Session session = (Session) threadLocal.get();
if (session == null) {
if (sessionFactory == null) {
try {
cfg.configure(CONFIG_FILE_LOCATION);
sessionFactory = cfg.buildSessionFactory();
}
catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
session = sessionFactory.openSession();
threadLocal.set(session);
}
return session;
}
首先判断threadLocal中是否存在Session对象 如果不存在 则创建Session对象 在创建Session对象时 首先要判断系统是否已经加载Configuration 如果没有sessionFactory 则需要先创建该对象 创建完成的Session对象 需要保存在threadLocal中以供本次访问线程的下一次调用
在关闭Session对象是 只需要从当前线程中取得Session对象 关闭该对象 并置空本地线程变量即可
public static void closeSession() throws HibernateException {
Session session = (Session) threadLocal.get();
threadLocal.set(null);
if (session != null) {
session.close();
}
}
来源:http://blog.chinaunix.net/uid-26284395-id-3049145.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java基础之堆内存溢出的解决
![](https://img.aspxhome.com/file/2023/8/59338_0s.png)
Java安全之Tomcat6 Filter内存马问题
![](https://img.aspxhome.com/file/2023/7/61027_0s.jpg)
java compare compareTo方法区别详解
详解ThreadLocal为什么会内存溢出原理
![](https://img.aspxhome.com/file/2023/3/59183_0s.png)
java字符串常用操作方法(查找、截取、分割)
C++右值引用与move和forward函数的使用详解
Java图片裁剪和生成缩略图的实例方法
![](https://img.aspxhome.com/file/2023/2/59342_0s.png)
从java中调用matlab详细介绍
![](https://img.aspxhome.com/file/2023/0/59870_0s.jpg)
浅谈springboot之JoinPoint的getSignature方法
Java实现按行读取大文件
SpringBoot 上传文件判空以及格式检验流程
![](https://img.aspxhome.com/file/2023/5/61405_0s.png)
Spring中的@Transactional的工作原理
![](https://img.aspxhome.com/file/2023/8/58028_0s.webp)
java设计模式之工厂模式实例详解
![](https://img.aspxhome.com/file/2023/2/60142_0s.png)
springboot自动扫描添加的BeanDefinition源码实例详解
![](https://img.aspxhome.com/file/2023/3/59943_0s.png)
一篇文章超详细的介绍Java继承
spring boot集成smart-doc自动生成接口文档详解
![](https://img.aspxhome.com/file/2023/3/60773_0s.png)
springBoot之如何获取接口请求数据和返回数据实现日志
springboot cloud使用eureka整合分布式事务组件Seata 的方法
![](https://img.aspxhome.com/file/2023/7/57957_0s.png)
Springboot项目快速实现过滤器功能
![](https://img.aspxhome.com/file/2023/9/57729_0s.png)
android仿微信联系人索引列表功能
![](https://img.aspxhome.com/file/2023/2/83512_0s.gif)