Spring整合Hibernate 二 - 声明式的事务管理
Spring大战Hibernate之声明式的事务管理
Spring配置文件:
添加事务管理类的bean:
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"><property name="sessionFactory" ref="sessionFactory" /> </bean>
该类由Spring提供,如果是hibernate4那就用"org.springframework.orm.hibernate4.HibernateTransactionManager"(Spring2不支持hibernate4)。
把sessionFactory注入给该bean的sessionFactory属性
配置事务的通知:
<tx:advice id="txAdvice" transaction-manager="txManager"><tx:attributes><tx:method name="getUser" read-only="true" /><tx:method name="add*" propagation="REQUIRED"/><tx:method name="save*" propagation="REQUIRED"/></tx:attributes> </tx:advice>
transaction-manager="txManager":这个事务通知所用的事务管理bean是刚才所定义的管理bean。
为名为 getUse 的方法添加事务。该事务是只读的(read-only="true")。
名字以 add、save 开头的方法添加事务。该事务传播特性为REQUIRED(propagation="REQUIRED")。
事务通知的属性解释:
一 propagation:事务的传播特性(支持程度),默认为REQUIRED
1.REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
2.SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。
3.MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。
4.REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。
5.NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
6.NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。
7.NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。
二 isolation:事务的隔离级别
1. ISOLATION_DEFAULT:使用数据库默认的事务隔离级别。
2. ISOLATION_READ_UNCOMMITTED:未提交读。这是事务最低的隔离级别,它充许别外一个事务可以看到这个事务未提交的数据。这种隔离级别会产生脏读,不可重复读和幻像读。
3. ISOLATION_READ_COMMITTED :提交读。保证一个事务修改的数据提交后才能被另外一个事务读取。另外一个事务不能读取该事务未提交的数据。这种事务隔离级别可以避免脏读出现,但是可能会出现不可重复读和幻像读。
4. ISOLATION_REPEATABLE_READ :重复读。这种事务隔离级别可以防止脏读,不可重复读。但是可能出现幻像读。
5. ISOLATION_SERIALIZABLE:存列化。这是花费最高代价但是最可靠的事务隔离级别。事务被处理为顺序执行。除了防止脏读,不可重复读外,还避免了幻读。
三 timeout:超时的时间(秒) -1:不限制
四 rollback-for:指定需要回滚的的异常类型,默认是针对unchecked exception回滚
添加事务的切入点
<aop:config><aop:pointcut id="txPointcut" expression="execution(public * com.startspring.service..*.*(..))" /><aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice" /> </aop:config>
指定切面为事务通知 txAdvice。指定切入点为txPointcut。
---------------------------------------------------------------------------------------------------------------
要使用<tx:advice> <aop:config> 等标签,需要先添加tx、aop命名空间,如下:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
-----------------------------------------------------------------------------------------------------------------
这时候可以把相应方法里事务开起和提交的语句去掉,让Spring来管理事务。如:
public void save(Student student) {Session session = sessionFactory.getCurrentSession();//session.beginTransaction(); session.save(student);//session.getTransaction().commit(); }
把session.beginTransaction();和session.getTransaction().commit();去掉。如果运作不报错则事务配置成功。
注意!!必须把hibernate的 hibernate.current_session_context_class 这项属性去掉!否则事务是不起效的!!也就是hibernate配置文件的<property name="current_session_context_class">thread</property> 或者Spring配置文件的 <prop key="hibernate.current_session_context_class">thread</prop>
转载于:https://www.cnblogs.com/likailan/p/3463372.html
Spring整合Hibernate 二 - 声明式的事务管理相关推荐
- Spring 整合 Mybatis - 二(切面、事务管理)
紧接着上篇<Spring 整合 Mybatis - 一(基础)>,介绍Spring 整合 Mybatis的切面.事务管理. 1 增加切面aop功能 1.1 spring.xml sprin ...
- 8.Spring整合Hibernate_2_声明式的事务管理(Annotation的方式)
声明式的事务管理(AOP的主要用途之一) (Annotation的方式) 1.加入annotation.xsd 2.加入txManager bean 3.<tx:annotation-drive ...
- Spring整合Hibernate的步骤
为什么要整合Hibernate? 1.使用Spring的IOC功能管理SessionFactory对象 LocalSessionFactoryBean 2.使用Spring管理Session对象 ...
- spring整合hibernate步骤及配置文件
spring整合hibernate,主要达到的目的有以下几点 1.使用Spring的IOC功能管理SessionFactory对象 --LocalSessionFactoryBean 对于Sessio ...
- 【Spring】Spring第三天 - 声明式事务、常用注解、Ajax 复习
一.自动注入 1.在Spring 配置文件中对象名和ref="id" . id 名相同,使用自动注入,可以不配置<property/> 2.两种配置办法 2.1 在&l ...
- spring整合hibernate事务编程中错误分析
2019独角兽企业重金招聘Python工程师标准>>> 在spring整合hibernate过程中,我们的配置文件: <?xml version="1.0" ...
- 解决在Spring整合Hibernate配置tx事务管理器出现错误的问题
解决在Spring整合Hibernate配置tx事务管理器出现错误的问题 参考文章: (1)解决在Spring整合Hibernate配置tx事务管理器出现错误的问题 (2)https://www.cn ...
- Spring整合Hibernate步骤以及遇到的问题
spring整合Hibernate步骤以及遇到的问题 文章目录 spring整合Hibernate步骤以及遇到的问题 步骤: 创建实体类: 编写Dao层: 编写server层: spring配置文件 ...
- 【Spring】Spring系列6之Spring整合Hibernate
6.Spring整合Hibernate 6.1.准备工作 6.2.示例 com.xcloud.entities.book com.xcloud.dao.book com.xcloud.service. ...
最新文章
- pyqt5 QtDesigner文件打开位置
- 实现 Java 多线程并发控制框架
- 金融风控实战——生肖属性单变量分析
- Scala学习(一)--Scala基础学习
- 从程序员到技术领导者
- css 盒模型 0302
- Mongodb在Windows下安装及配置
- Spring中事务管理的几种配法
- 优麒麟系统安装MySQL_优麒麟系统安装教程-电脑系统安装手册
- SAP HANA XS 专栏
- 昆仑万维上半年营收22.5亿同比降2%:净利6.4亿 同比降35.6%
- jCarouselLite——传送带(多图)
- 按规定顺序输出26个字母
- 秘辛:2019上半年程序员生存报告
- 深入剖析ReentrantLock公平锁与非公平锁源码实现
- 企业微信---第三方应用开发 笔记
- 一位网友的网络求职经历
- 【干货】抖音、快手、火山短视频去水印软件使用介绍
- Debian内核防毒AntiVir安装
- 乌班图安装python_优麒麟/Ubuntu安装Python3