spring设置回滚事务属性
2017-11-07 21:00
519 查看
设置回滚事务属性 :spring事务 运行过程中 碰到运行时异常 自动回滚 非运行时异常不会回滚 rollback-for="" 指定会自动回滚的非运行时异常 no-rollback-for="" 指定某些运行时异常抛出时 不回滚 <!-- 事务管理类 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> 例如 <tx:advice id="myAdvise" transaction-manager="transactionManager"> <tx:attributes> <!--propagation:定义事务传播属性(要在同一个事务中,出现错误才回一起回滚)--> <!--rollback-for:定义出现何种错误事务回滚--> <tx:method name="Update*" propagation="REQUIRED" rollback-for="Exception"/> </tx:attributes> </tx:advice>
<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
">
<!-- 扫描注解 -->
<context:component-scan base-package="lesson04.testm"></context:component-scan>
<!-- 读取properties资源文件 -->
<context:property-placeholder location="classpath:/lesson04/jdbc/jdbcoracle.properties"/>
<!-- ${username}是个关键字 默认获取操作系统用户 -->
<!-- 连接数据库 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${url}"></property>
<property name="username" value="${username1}"></property>
<property name="password" value="${password}"></property>
<property name="driverClassName" value="${driverClass}"></property>
</bean>
<!-- jdbc的模板 可以执行sql语句 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<!-- 把连接数据库的bean注入 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理类 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 事务需要操作数据库 要把连接数据库的bean注入 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 定义通知 通知的代码 spring已经实现 -->
<!-- 上面的id名为 id="transactionManager" 下面的 transaction-manager="transactionManager"可以不写-->
<tx:advice id="myAdvise" transaction-manager="transactionManager">
<tx:attributes>
<!--
设置回滚事务属性 :spring事务 运行过程中 碰到运行时异常 自动回滚 非运行时异常不会回滚
rollback-for="" 指定会自动回滚的非运行时异常 设值成Exception就可以了 发生任何异常都回滚
no-rollback-for="" 指定某些运行时异常抛出时 不回滚
-->
<tx:method name="Update*" propagation="REQUIRED" rollback-for="Exception" isolation="DEFAULT" />
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="delete*"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 定义切点 -->
<aop:config>
<!-- 任意返回值 lesson04.testm.dao包下的所有类、所有方法拦截 -->
<aop:pointcut expression="execution(* lesson04.testm.dao.*.*(..))" id="myPoint"/>
<!-- 把切点和通知绑定 <tx:advice> -->
<aop:advisor advice-ref="myAdvise" pointcut-ref="myPoint"/>
</aop:config>
</beans>
相关文章推荐
- spring设置隔离事务属性
- spring 事务控制 设置手动回滚
- 使用spring声明式事务,spring使用AOP来支持声明式事务,会根据事务属性,自动在方法调用之前决定是否开启一个事务,并在方法执行之后决定事务提交或回滚事务。
- 关于spring中注入连接池,事务管理等有关的属性设置以后可能用得到
- Spring事务传播隔离只读回滚超时5种属性详细
- 详解spring: 事务属性的设置
- spring手动设置事务回滚
- Spring事务传播隔离只读回滚超时5种属性详细
- Spring如何设置让事务自动提交和回滚?Spring两种事务管理方式的配置及使用
- Spring中Transactional不添加rollbackFor等属性时的事务回滚问题
- spring的事务管理的原理(如何设置在Action或者在Service层进行事务拦截及其回滚)
- Spring中事务的其他属性(隔离级别、回滚、只读、过期)
- spring事务传播属性和隔离级别
- Spring中的事务传播属性详解
- Spring配置事务中@Transactional各个属性定义
- Spring 事务回滚机制
- Spring事务属性
- Spring事务属性
- [spring,mysql] spring使用注解式事务声明(@Transactional)无法回滚
- Spring的AOP事务管理默认针对unchecked异常回滚