hibernate中事务隔离级别

  • 1:读未提交
  • 2:读已提交
  • 4:可重复读
  • 8:可串行化

hibernate事务使用

  1. 在核心配置文件中配置事务隔离级别
    1. <property name="hibernate.connection.isolation">4</property>
  2. 确保一个逻辑事务中的session是同一个
    1. 核心配置文件中配置事务当前线程绑定session:<property name="hibernate.current_session_context_class" >thread</property>
    2. 通过sessionFactory.getCurrentSession()来获取session(getCurrentSession()方法默认不开启,必须配置事务当前线程绑定session来开启)
  3. 注:使用getCurrentSession()方法获取的session操作完不需要关闭,线程结束会自动关闭

核心配置文件hibernate.cfg.xml

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url" >jdbc:mysql:///test02</property>
<property name="hibernate.connection.username" >root</property>
<property name="hibernate.connection.password" >root</property>
<property name="hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property> <property name="hibernate.hbm2ddl.auto" >create</property>
<property name="hibernate.show_sql" >true</property>
<!-- <property name="hibernate.format_sql" >true</property> --> <property name="hibernate.connection.isolation">4</property>
<property name="hibernate.current_session_context_class" >thread</property> <mapping resource="com/qf/entity/Teacher.hbm.xml"/>
<mapping resource="com/qf/entity/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>

sessionFactory.getCurrentSession()方法的实现

内部使用了ThreadLocal来实现线程绑定session

public Session getCurrentSession() throws HibernateException {
if ( currentSessionContext == null ) {
throw new HibernateException( "No CurrentSessionContext configured!" );
}
return currentSessionContext.currentSession();
} 
@Override
public final Session currentSession() throws HibernateException {
Session current = existingSession( factory() );
if ( current == null ) {
current = buildOrObtainSession();
// register a cleanup sync
current.getTransaction().registerSynchronization( buildCleanupSynch() );
// wrap the session in the transaction-protection proxy
if ( needsWrapping( current ) ) {
current = wrap( current );
}
// then bind it
doBind( current, factory() );
}
else {
validateExistingSession( current );
}
return current;
}

1. 测试getCurrentSession()方法获取的session操作完是否会自己关闭

@Test
public void test() {
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory();
Session session = factory.getCurrentSession(); Transaction tx = session.beginTransaction(); Student student = session.get(Student.class, 1L);
System.out.println(student);
tx.commit();
session.close();
}

-------------------------------------Junit-----------------------------------------

org.hibernate.SessionException: Session was already closed

线程结束,session已经自己关闭了,不需要再去手动关闭

2. 测试两次通过getCurrentSession()方法获取的session是否一致

@Test
public void test() {
Configuration cfg = new Configuration().configure();
SessionFactory factory = cfg.buildSessionFactory(); Session session1 = factory.getCurrentSession();
Session session2 = factory.getCurrentSession();
System.out.println("两次通过getCurrentSession()获取的session是否一致:"+(session1==session2)); Session session3 = factory.openSession();
Session session4 = factory.openSession();
System.out.println("两次通过openSession()获取的session是否一致:"+(session3==session4));
} 

-------------------------------------console-----------------------------------------

两次通过getCurrentSession()获取的session是否一致:true
两次通过openSession()获取的session是否一致:false

  

七、hibernate的事务使用的更多相关文章

  1. Hibernate中事务的隔离级别设置

    Hibernate中事务的隔离级别,如下方法分别为1/2/4/8. 在Hibernate配置文件中设置,设置代码如下

  2. (转载)Hibernate的事务管理

    Hibernate的事务管理 事务(Transaction)是工作中的基本逻辑单位,可以用于确保数据库能够被正确修改,避免数据只修改了一部分而导致数据不完整,或者在修改时受到用户干扰.作为一名软件设计 ...

  3. atitit.spring hibernate的事务机制 spring不能保存对象的解决

    atitit.spring hibernate的事务机制 spring不能保存对象的解决 sessionFactory.openSession() 不能..log黑头马sql语言.. sessionF ...

  4. Hibernate学习笔记(三)—— Hibernate的事务控制

    Hibernate是对JDBC的轻量级封装,其主要功能是操作数据库.在操作数据库过程中,经常会遇到事务处理的问题,接下来就来介绍Hibernate中的事务管理. 在学习Hibernate中的事务处理之 ...

  5. Hibernate的事务管理

    Hibernate的事务管理 事务(Transaction)是工作中的基本逻辑单位,可以用于确保数据库能够被正确修改,避免数据只修改了一部分而导致数据不完整,或者在修改时受到用户干扰.作为一名软件设计 ...

  6. JavaWeb_(Hibernate框架)Hibernate中事务

    Hibernate中事务 事务的性质 事物的隔离级别 配置事务的隔离级别 事务的性质 原子性:原子,不可再分,一个操作不能分为更小的操作,要么全都执行,要么全不执行. 一致性:事务在完成时,必须使得所 ...

  7. 事务种类jdbc,Hibernate,JTA事务

    JDBC事务 String URL="jdbc:sqlserver://localhost:1433;databaseName=test2"; String USER=" ...

  8. Hibernate中事务声明

    Hibernate中JDBC事务声明,在Hibernate配置文件中加入如下代码,不做声明Hibernate默认就是JDBC事务. 一个JDBC 不能跨越多个数据库. Hibernate中JTA事务声 ...

  9. 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】

    一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...

随机推荐

  1. 98-基于FPGA Spartan6 的双路光纤PCIe采集卡(2路光纤卡) 光纤PCIe卡

    1.板卡概述 板卡采用xilinx Spartan6系列芯片,支持 PCI Express Base Specification 1.1 x1.内含丰富的逻辑资源和存储单元,板卡FPGA外接双片32M ...

  2. sass-RGB颜色函数-RGB()颜色函数

    在 Sass 的官方文档中,列出了 Sass 的颜色函数清单,从大的方面主要分为 RGB , HSL 和 Opacity 三大函数,当然其还包括一些其他的颜色函数,比如说 adjust-color 和 ...

  3. mysql小知识

    char(10): 换行符 char(13): 回车符 UPDATE tablename SET field = REPLACE(REPLACE(field, CHAR(10), ”), CHAR(1 ...

  4. Hadoop中的排序和连接

    MapReduce的全排序 主要是为了保证分区排序,即第一个分区的最后一个Key值小于第二个分区的第一个Key值 与普通的排序仅仅多一个自定义分区类MyPartitioner见自己所写的实验 (设置一 ...

  5. 开源大数据生态下的 Flink 应用实践

    过去十年,面向整个数字时代的关键技术接踵而至,从被人们接受,到开始步入应用.大数据与计算作为时代的关键词已被广泛认知,算力的重要性日渐凸显并发展成为企业新的增长点.Apache Flink(以下简称 ...

  6. 【C++11新特性】 C++11智能指针之shared_ptr

    C++中的智能指针首先出现在“准”标准库boost中.随着使用的人越来越多,为了让开发人员更方便.更安全的使用动态内存,C++11也引入了智能指针来管理动态对象.在新标准中,主要提供了shared_p ...

  7. Python3解leetcode Lowest Common Ancestor of a Binary Search Tree

    问题描述: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in ...

  8. webpack使用问题记录

    1.版本问题 webpack的安装并不是越新越好.当使用最新版本时不使用配置文件,直接编译不生成文件甚至报错:使用3.12版本时编译文件可以成功但是不生成新文件:使用 3.5.3 版本时成功且生成新文 ...

  9. vue搭配的UI框架 pc端 + 移动端

    PC桌面端UI框架: 1,iview      (最新,用户评分高功能多炫酷 解决和避免了其他UI框架出现的一些小问题) 2, bootstrap  (使用用户最多样式死板没特色) 3,Element ...

  10. Nuget-Doc:Nuget 简介

    ylbtech-Nuget-Doc:Nuget 简介 1.返回顶部 1. NuGet 简介 2019/05/24 适用于任何现代开发平台的基本工具可充当一种机制,通过这种机制,开发人员可以创建.共享和 ...