七、hibernate的事务使用
hibernate中事务隔离级别
- 1:读未提交
- 2:读已提交
- 4:可重复读
- 8:可串行化
hibernate事务使用
- 在核心配置文件中配置事务隔离级别
- <property name="hibernate.connection.isolation">4</property>
- 确保一个逻辑事务中的session是同一个
- 核心配置文件中配置事务当前线程绑定session:<property name="hibernate.current_session_context_class" >thread</property>
- 通过sessionFactory.getCurrentSession()来获取session(getCurrentSession()方法默认不开启,必须配置事务当前线程绑定session来开启)
- 注:使用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的事务使用的更多相关文章
- Hibernate中事务的隔离级别设置
Hibernate中事务的隔离级别,如下方法分别为1/2/4/8. 在Hibernate配置文件中设置,设置代码如下
- (转载)Hibernate的事务管理
Hibernate的事务管理 事务(Transaction)是工作中的基本逻辑单位,可以用于确保数据库能够被正确修改,避免数据只修改了一部分而导致数据不完整,或者在修改时受到用户干扰.作为一名软件设计 ...
- atitit.spring hibernate的事务机制 spring不能保存对象的解决
atitit.spring hibernate的事务机制 spring不能保存对象的解决 sessionFactory.openSession() 不能..log黑头马sql语言.. sessionF ...
- Hibernate学习笔记(三)—— Hibernate的事务控制
Hibernate是对JDBC的轻量级封装,其主要功能是操作数据库.在操作数据库过程中,经常会遇到事务处理的问题,接下来就来介绍Hibernate中的事务管理. 在学习Hibernate中的事务处理之 ...
- Hibernate的事务管理
Hibernate的事务管理 事务(Transaction)是工作中的基本逻辑单位,可以用于确保数据库能够被正确修改,避免数据只修改了一部分而导致数据不完整,或者在修改时受到用户干扰.作为一名软件设计 ...
- JavaWeb_(Hibernate框架)Hibernate中事务
Hibernate中事务 事务的性质 事物的隔离级别 配置事务的隔离级别 事务的性质 原子性:原子,不可再分,一个操作不能分为更小的操作,要么全都执行,要么全不执行. 一致性:事务在完成时,必须使得所 ...
- 事务种类jdbc,Hibernate,JTA事务
JDBC事务 String URL="jdbc:sqlserver://localhost:1433;databaseName=test2"; String USER=" ...
- Hibernate中事务声明
Hibernate中JDBC事务声明,在Hibernate配置文件中加入如下代码,不做声明Hibernate默认就是JDBC事务. 一个JDBC 不能跨越多个数据库. Hibernate中JTA事务声 ...
- 【Java EE 学习 53】【Spring学习第五天】【Spring整合Hibernate】【Spring整合Hibernate、Struts2】【问题:整合hibernate之后事务不能回滚】
一.Spring整合Hibernate 1.如果一个DAO 类继承了HibernateDaoSupport,只需要在spring配置文件中注入SessionFactory就可以了:如果一个DAO类没有 ...
随机推荐
- 2018-8-10-win10-UWP-序列化
title author date CreateTime categories win10 UWP 序列化 lindexi 2018-08-10 19:16:50 +0800 2018-2-13 17 ...
- c++消息中间件
ZeroMQ ActiveMQ-CPP 另外 ZeroMQ 的作者用 C 重构了一套.改名叫:nanomsg ZeroMQ:https://www.cnblogs.com/rainbowzc/p/33 ...
- 第01章 Spring概述
第01章 Spring概述 1.Spring概述 ①Spring是一个开源框架 ②Spring为简化企业级开发而生,使用Spring,JavaBean就可以实现很多以前要靠EJB才能实现的功能.同样的 ...
- BZOJ3622 已经没有什么好害怕的了 二项式反演+DP
题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=3622 题解 首先显然如果 \(n - k\) 为奇数那么就是无解.否则的话,"糖果& ...
- Linux下安装gnuplot
sudo apt-get install gnuplot 但是在 terminal 里面输入: gnuplot 提示 Terminal type set to unknown.解决方法是安装 x11: ...
- C#后台获取post参数
public static string GetQueryString(string key) { if (HttpContext.Current.Request[key] == null) retu ...
- BZOJ 5137: [Usaco2017 Dec]Standing Out from the Herd(后缀自动机)
传送门 解题思路 这个似乎和以前做过的一道题很像,只不过这个是求本质不同子串个数.肯定是先把广义\(SAM\)造出来,然后\(dfs\)时把子节点的信息合并到父节点上,看哪个只被一个串覆盖,\(ans ...
- Django中的get()和filter()区别
前言 在django中,我们查询经常用的两个API中,会经常用到get()和filter()两个方法,两者的区别是什么呢? object.get()我们得到的是一个对象,如果在数据库中查不到这个对象或 ...
- (转)Windows下安装Docker, GitBash环境配置
转:https://blog.csdn.net/chengly0129/article/details/68944269 官网介绍: https://docs.docker.com/toolbox/t ...
- obj文件中的关键字
obj文件使用的关键字 关键字 含义 v 表示本行指定一个顶点,此关键字后跟着3个单精度浮点数,分别表示该顶点的X.Y.Z坐标值 vt 表示本行指定一个纹理坐标,此关键字后跟着两个单精度浮点数,分别表 ...