Hibernate 自身提供了三种管理 Session 对象的方法

  • Session 对象的生命周期与本地线程绑定
  • Session 对象的生命周期与 JTA 事务绑定
  • Hibernate 托付程序管理 Session 对象的生命周期

在 Hibernate 的配置文件里, hibernate.current_session_context_class 属性用于指定 Session 管理方式, 可选值包含
  • thread: Session 对象的生命周期与本地线程绑定
  • jta*: Session 对象的生命周期与 JTA 事务绑定
  • managed: Hibernate 托付程序来管理 Session 对象的生命周期

Session 对象的生命周期与本地线程绑定

假设把 Hibernate 配置文件的 hibernate.current_session_context_class 属性值设为 thread, Hibernate 就会依照与本地线程绑定的方式来管理 Session
Hibernate 按下面规则把 Session 与本地线程绑定
  • 当一个线程(threadA)第一次调用 SessionFactory 对象的 getCurrentSession() 方法时, 该方法会创建一个新的 Session(sessionA) 对象, 把该对象与 threadA 绑定, 并将 sessionA 返回
  • 当 threadA 再次调用 SessionFactory 对象的 getCurrentSession() 方法时, 该方法将返回 sessionA 对象
  • 当 threadA 提交 sessionA 对象关联的事务时, Hibernate 会自己主动flush sessionA 对象的缓存, 然后提交事务, 关闭 sessionA 对象. 当 threadA 撤销 sessionA 对象关联的事务时, 也会自己主动关闭 sessionA 对象
  • 若 threadA 再次调用 SessionFactory 对象的 getCurrentSession() 方法时, 该方法会又创建一个新的 Session(sessionB) 对象, 把该对象与 threadA 绑定, 并将 sessionB 返回


代码具体解释:
HibernateUtils.java(单例)
package com.atguigu.hibernate.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder; public class HibernateUtils { private HibernateUtils(){} private static HibernateUtils instance = new HibernateUtils(); public static HibernateUtils getInstance() {
return instance;
} private SessionFactory sessionFactory; public SessionFactory getSessionFactory() {
if (sessionFactory == null) {
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry = new ServiceRegistryBuilder()
.applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
}
return sessionFactory;
} public Session getSession(){
return getSessionFactory().getCurrentSession();
} }

DepartmentDao.java

package com.atguigu.hibernate.dao;

import org.hibernate.Session;

import com.atguigu.hibernate.entities.Department;
import com.atguigu.hibernate.hibernate.HibernateUtils; public class DepartmentDao { public void save(Department dept){
//内部获取 Session 对象
//获取和当前线程绑定的 Session 对象
//1. 不须要从外部传入 Session 对象
//2. 多个 DAO 方法也能够使用一个事务!
Session session = HibernateUtils.getInstance().getSession();
System.out.println(session.hashCode()); session.save(dept);
} /**
* 若须要传入一个 Session 对象, 则意味着上一层(Service)须要获取到 Session 对象.
* 这说明上一层须要和 Hibernate 的 API 紧密耦合. 所以不推荐使用此种方式.
*/
public void save(Session session, Department dept){
session.save(dept);
} }

Test

package com.atguigu.hibernate.test;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List; import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.criterion.Conjunction;
import org.hibernate.criterion.Disjunction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.jdbc.Work;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.service.ServiceRegistryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; import com.atguigu.hibernate.dao.DepartmentDao;
import com.atguigu.hibernate.entities.Department;
import com.atguigu.hibernate.entities.Employee;
import com.atguigu.hibernate.hibernate.HibernateUtils; public class HibernateTest { private SessionFactory sessionFactory;
private Session session;
private Transaction transaction; @Before
public void init(){
Configuration configuration = new Configuration().configure();
ServiceRegistry serviceRegistry =
new ServiceRegistryBuilder().applySettings(configuration.getProperties())
.buildServiceRegistry();
sessionFactory = configuration.buildSessionFactory(serviceRegistry); session = sessionFactory.openSession();
transaction = session.beginTransaction();
} @After
public void destroy(){
transaction.commit();
session.close();
sessionFactory.close();
} @Test
public void testManageSession(){ //获取 Session
//开启事务
Session session = HibernateUtils.getInstance().getSession();
System.out.println("-->" + session.hashCode());
Transaction transaction = session.beginTransaction(); DepartmentDao departmentDao = new DepartmentDao(); Department dept = new Department();
dept.setName("ATGUIGU"); departmentDao.save(dept);
departmentDao.save(dept);
departmentDao.save(dept); //若 Session 是由 thread 来管理的, 则在提交或回滚事务时, 已经关闭 Session 了.
transaction.commit();
System.out.println(session.isOpen());
} }

hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory> <!-- Hibernate 连接数据库的基本信息 -->
<property name="connection.username">scott</property>
<property name="connection.password">java</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property> <!-- Hibernate 的基本配置 -->
<!-- Hibernate 使用的数据库方言 -->
<property name="dialect">org.hibernate.dialect.Oracle10gDialect</property> <!-- 执行时是否打印 SQL -->
<property name="show_sql">true</property> <!-- 执行时是否格式化 SQL -->
<property name="format_sql">true</property> <!-- 生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property> <!-- 设置 Hibernate 的事务隔离级别 -->
<property name="connection.isolation">2</property> <!-- 删除对象后, 使其 OID 置为 null -->
<property name="use_identifier_rollback">true</property> <!-- 配置 C3P0 数据源 -->
<!--
<property name="hibernate.c3p0.max_size">10</property>
<property name="hibernate.c3p0.min_size">5</property>
<property name="c3p0.acquire_increment">2</property> <property name="c3p0.idle_test_period">2000</property>
<property name="c3p0.timeout">2000</property> <property name="c3p0.max_statements">10</property>
--> <!-- 设定 JDBC 的 Statement 读取数据的时候每次从数据库中取出的记录条数 -->
<property name="hibernate.jdbc.fetch_size">100</property> <!-- 设定对数据库进行批量删除,批量更新和批量插入的时候的批次大小 -->
<property name="jdbc.batch_size">30</property> <!-- 启用二级缓存 -->
<property name="cache.use_second_level_cache">true</property> <!-- 配置使用的二级缓存的产品 -->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property> <!-- 配置启用查询缓存 -->
<property name="cache.use_query_cache">true</property> <!-- 配置管理 Session 的方式 -->
<property name="current_session_context_class">thread</property> <!-- 须要关联的 hibernate 映射文件 .hbm.xml -->
<mapping resource="com/atguigu/hibernate/entities/Department.hbm.xml"/>
<mapping resource="com/atguigu/hibernate/entities/Employee.hbm.xml"/> <class-cache usage="read-write" class="com.atguigu.hibernate.entities.Employee"/>
<class-cache usage="read-write" class="com.atguigu.hibernate.entities.Department"/>
<collection-cache usage="read-write" collection="com.atguigu.hibernate.entities.Department.emps"/>
</session-factory>
</hibernate-configuration>

hibernate 管理 Session(单独使用session,非spring)的更多相关文章

  1. hibernate 管理 Session(单独使用session,不spring)

    Hibernate 本身提供了三个管理 Session 对象的方法 Session 对象的生命周期与本地线程绑定 Session 对象的生命周期与 JTA 事务绑定 Hibernate 托付程序管理 ...

  2. 为什么要用Hibernate框架? 把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了?

    既然用Hibernate框架访问管理持久层,那为何又提到用Spring来管理以及整合Hibernate呢?把SessionFactory,Session,Transcational封装成包含crud的 ...

  3. Hibernate管理Session和批量操作

    Hibernate管理Session Hibernate自身提供了三种管理Session对象的方法 Session对象的生命周期与本地线程绑定 Session对象的生命周期与JTA事务绑定 Hiber ...

  4. Spring与Hibernate集成中的Session问题

    主要讨论Spring与Hibernate集成中的session问题 1.通过getSession()方法获得session进行操作 public class Test extends Hibernat ...

  5. spring+hibernate管理多个数据源(非分布式事务)

    本文通过一个demo,介绍如何使用spring+hibernate管理多个数据源,注意,本文的事务管理并非之前博文介绍的分布式事务. 这个demo将使用两个事务管理器分别管理两个数据源.对于每一个独立 ...

  6. 框架Hibernate笔记系列 基础Session

    标题:框架Hibernate笔记 资料地址: 1. www.icoolxue.com 孔浩 1.背景简介 Hibenate是JBoss公司的产品.它是数据持久化的框架.Usually,我们使用JDBC ...

  7. 79. could not initialize proxy - no Session 【从零开始学Spring Boot】

    [原创文章,转载请注明出处] Spring与JPA结合时,如何解决懒加载no session or session was closed!!! 实际上Spring Boot是默认是打开支持sessio ...

  8. Hibernate学习---Configuration,Session,SessionFactory

    上一节我们讲到了Hibernate的测试,并且给出了测试代码,刚开始看见这个测试代码的同学估计是一头雾水把,所以这一节我们来讲一下测试代码. 本节主要内容: Configuration Session ...

  9. 浅谈hibernate的sessionFactory和session

    一.hibernate是什么? Hibernate是一个开放源代码的对象关系映射框架,它对JDBC进行了非常轻量级的对象封装,使得Java程序员可以随心所欲的使用对象编程思维来操纵数据库. Hiber ...

随机推荐

  1. Spring整合JUnit4测试使用注解引入多个配置文件

    转自:https://kanpiaoxue.iteye.com/blog/2151903 我们使用spring写junit单测的时候,有的时候我们的spring配置文件只有一个.我们在类的注释上面会这 ...

  2. 带中横线的日期格式在iOS手机系统上 转换时间戳NaN问题

    类似于 '2019-04-01 14:13:00' 这样的日期格式转换时间戳在iOS手机上是无法转换的,需要先处理日期格式成 '2019/04/01 14:13:00' var str = '2019 ...

  3. 从Oracle同步数据到SQLServer——大小写敏感设置

    Oracle默认是大小写敏感,而SQLServer默认大小写不敏感, 尤其是涉及主键字段时,注意请提前设置SQLServer对应的数据库表为大小写敏感,不然会报主键冲突的错误. 设置表内大小写敏感 A ...

  4. C#使用SqlTransaction事务回滚与SqlBulkCopy批量插入数据

    C#中批量处理数据,有时候因为一条记录导致整个批量处理失败.这时候肯能会导致数据不全等问题,这时候我们可以使用SqlTransaction来进行事务回滚,即是要么全部成功要么全部不成功.如下代码 // ...

  5. 第三课 创建函数 - 从EXCEL读取 - 导出到EXCEL - 异常值 - Lambda函数 - 切片和骰子数据

    第 3 课   获取数据 - 我们的数据集将包含一个Excel文件,其中包含每天的客户数量.我们将学习如何对 excel 文件进​​行处理.准备数据 - 数据是有重复日期的不规则时间序列.我们将挑战数 ...

  6. [OpenWrt]安装mjpg-streamer

    安装mjpg-streamer 远程监控基本上是wifi小车的一个必备功能了.摄像头我用的是奥尼百脑通 D881,这个要100左右. 确认安装了以下软件: kmod-usb2 kmod-video-u ...

  7. JVM内存划分以及值传递和引用传递的区别

    Day05_SHJavaTraing_4-8-2017 一.JVM对自己的内存划分为5个区域    1.方法栈:所有的方法运行的时候进入内存    2.堆:存储的是容器和对象    3.方法和数据共享 ...

  8. VS命令行的使用

    CD 命令是改变当前路径,但是它不会改变当前盘符,改变盘符要输入 [盘符]: 命令. 如下: Setting environment for using Microsoft Visual Studio ...

  9. VC++抛出自定义编译期异常的指令

    #error 即可, 抛出消息是 #pragma message 最新的还有static_assert有一些用 一下子忘了网上居然搜不到...尝试了 关键字vc++.vc.vs.msvc +  抛出编 ...

  10. Lazy Initialization with Swift

    Lazy initialization (also sometimes called lazy instantiation, or lazy loading) is a technique for d ...