Spirng中提供的事务AOP
PlatformTransactionManager 接口提供事务操作的方法
- DataSourceTransactionManager: JDBC或者iBatis的事务处理器
- ItibernateTransactionManager :Hibernate的事务处理器
Spirng中XML设
头文件设置
<?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/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
配置事务管理器
<bean id="dataSourceTransactionManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dateSource"></property>
</bean>//需要导入spring-jdb架包
配置事务通知
<tx:advice id="txAdvice" transaction-manager="dataSourceTransactionManage">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
</tx:attributes>
</tx:advice>
配置method中name可以使用通配符,是全部,find是以find开头的全部
内有标签
isolation:指定事务隔离级别
propagation:指定传播行为
REQUIRED:支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
SUPPORTS:支持当前事务,如果当前没有事务,就以非事务方式执行。
MANDATORY:支持当前事务,如果当前没有事务,就抛出异常。
REQUIRES_NEW:新建事务,如果当前存在事务,把当前事务挂起。
NOT_SUPPORTED:以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
NEVER:以非事务方式执行,如果当前存在事务,则抛出异常。 =
NESTED:支持当前事务,如果当前事务存在,则执行一个嵌套事务,如果当前没有事务,就新建一个事务。
read-only:用于指定事务是否只读,默认为true为只读
timeout:超时时间,默认-1没有超时设置,单位秒
rollback-for:指定一个异常,出现后回滚
no-rollback-for:除指定的异常,其他异常出现全部回滚
配置切片
<aop:config>
<aop:pointcut id="pt1" expression="execution( * com.Service.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>