小结:用Spring AOP配置事务要注意的几项
Spring AOP形式管理事务,Spring的官方文档写得不全,容易漏配,特总结如下:
1,数据源要加上数据源事务代理
Xml代码
<!-- 默认的数据源配置 -->
<bean id="talent.defaultDataSourceTarget"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- org.apache.commons.dbcp.BasicDataSource -->
<!-- org.springframework.jdbc.datasource.DriverManagerDataSource -->
<property name="driverClassName"
value="${jdbc.default.driverClassName}"/>
<property name="url" value="${jdbc.default.url}"/>
<property name="username" value="${jdbc.default.username}"/>
<property name="password" value="${jdbc.default.password}"/>
</bean>
<!-- 数据源代理 -->
<bean id="talent.defaultDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg>
<ref bean="talent.defaultDataSourceTarget" />
</constructor-arg>
</bean>
<!-- 默认的数据源配置 -->
<bean id="talent.defaultDataSourceTarget"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- org.apache.commons.dbcp.BasicDataSource -->
<!-- org.springframework.jdbc.datasource.DriverManagerDataSource -->
<property name="driverClassName"
value="${jdbc.default.driverClassName}"/>
<property name="url" value="${jdbc.default.url}"/>
<property name="username" value="${jdbc.default.username}"/>
<property name="password" value="${jdbc.default.password}"/>
</bean>
<!-- 数据源代理 -->
<bean id="talent.defaultDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg>
<ref bean="talent.defaultDataSourceTarget" />
</constructor-arg>
</bean>
2,事务特性配置时,要注明rollback-for类型,并不是所有的异常都回滚的
Xml代码
<!-- 配置事务特性 -->
<tx:advice id="serviceAdvice"
transaction-manager="talent.defaultTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务特性 -->
<tx:advice id="serviceAdvice"
transaction-manager="talent.defaultTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
3,配置哪些类的方法需要进行事务管理时,表达式要写对
Xml代码
<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="servicePointcut" expression="execution(* com.jstrd.talent.manager.*.*(..))"/>
<aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice"/>
</aop:config>
<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="servicePointcut" expression="execution(* com.jstrd.talent.manager.*.*(..))"/>
<aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice"/>
</aop:config>
此处只对com.jstrd.talent.manager包下的类进行管理,并不会对其子包也进行管理的
4,要通过ctx.getBean("beanName")的形式来获取管理类,而不是new一个管理类出来
Spring的声明式事务管理<tx:advice/> 有关的设置
<tx:advice/> 有关的设置
这一节里将描述通过 <tx:advice/> 标签来指定不同的事务性设置。默认的 <tx:advice/> 设置如下:
事务传播设置是 REQUIRED
隔离级别是 DEFAULT
事务是 读/写
事务超时默认是依赖于事务系统的,或者事务超时没有被支持。
任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚
这些默认的设置当然也是可以被改变的。 <tx:advice/> 和 <tx:attributes/> 标签里的 <tx:method/> 各种属性设置总结如下:
表 9.1. <tx:method/> 有关的设置
属性 是否需要? 默认值 描述
name 是 与事务属性关联的方法名。通配符(*)可以用来指定一批关联到相同的事务属性的方法。 如:'get*'、'handle*'、'on*Event'等等。
propagation 不 REQUIRED 事务传播行为
isolation 不 DEFAULT 事务隔离级别
timeout 不 -1 事务超时的时间(以秒为单位)
read-only 不 false 事务是否只读?
rollback-for 不 将被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException,ServletException'
no-rollback-for 不 不 被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException
Spring AOP形式管理事务,Spring的官方文档写得不全,容易漏配,特总结如下:
1,数据源要加上数据源事务代理
Xml代码
<!-- 默认的数据源配置 -->
<bean id="talent.defaultDataSourceTarget"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- org.apache.commons.dbcp.BasicDataSource -->
<!-- org.springframework.jdbc.datasource.DriverManagerDataSource -->
<property name="driverClassName"
value="${jdbc.default.driverClassName}"/>
<property name="url" value="${jdbc.default.url}"/>
<property name="username" value="${jdbc.default.username}"/>
<property name="password" value="${jdbc.default.password}"/>
</bean>
<!-- 数据源代理 -->
<bean id="talent.defaultDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg>
<ref bean="talent.defaultDataSourceTarget" />
</constructor-arg>
</bean>
<!-- 默认的数据源配置 -->
<bean id="talent.defaultDataSourceTarget"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- org.apache.commons.dbcp.BasicDataSource -->
<!-- org.springframework.jdbc.datasource.DriverManagerDataSource -->
<property name="driverClassName"
value="${jdbc.default.driverClassName}"/>
<property name="url" value="${jdbc.default.url}"/>
<property name="username" value="${jdbc.default.username}"/>
<property name="password" value="${jdbc.default.password}"/>
</bean>
<!-- 数据源代理 -->
<bean id="talent.defaultDataSource" class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
<constructor-arg>
<ref bean="talent.defaultDataSourceTarget" />
</constructor-arg>
</bean>
2,事务特性配置时,要注明rollback-for类型,并不是所有的异常都回滚的
Xml代码
<!-- 配置事务特性 -->
<tx:advice id="serviceAdvice"
transaction-manager="talent.defaultTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务特性 -->
<tx:advice id="serviceAdvice"
transaction-manager="talent.defaultTransactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
3,配置哪些类的方法需要进行事务管理时,表达式要写对
Xml代码
<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="servicePointcut" expression="execution(* com.jstrd.talent.manager.*.*(..))"/>
<aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice"/>
</aop:config>
<!-- 配置哪些类的方法需要进行事务管理 -->
<aop:config proxy-target-class="true">
<aop:pointcut id="servicePointcut" expression="execution(* com.jstrd.talent.manager.*.*(..))"/>
<aop:advisor pointcut-ref="servicePointcut" advice-ref="serviceAdvice"/>
</aop:config>
此处只对com.jstrd.talent.manager包下的类进行管理,并不会对其子包也进行管理的
4,要通过ctx.getBean("beanName")的形式来获取管理类,而不是new一个管理类出来
Spring的声明式事务管理<tx:advice/> 有关的设置
<tx:advice/> 有关的设置
这一节里将描述通过 <tx:advice/> 标签来指定不同的事务性设置。默认的 <tx:advice/> 设置如下:
事务传播设置是 REQUIRED
隔离级别是 DEFAULT
事务是 读/写
事务超时默认是依赖于事务系统的,或者事务超时没有被支持。
任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚
这些默认的设置当然也是可以被改变的。 <tx:advice/> 和 <tx:attributes/> 标签里的 <tx:method/> 各种属性设置总结如下:
表 9.1. <tx:method/> 有关的设置
属性 是否需要? 默认值 描述
name 是 与事务属性关联的方法名。通配符(*)可以用来指定一批关联到相同的事务属性的方法。 如:'get*'、'handle*'、'on*Event'等等。
propagation 不 REQUIRED 事务传播行为
isolation 不 DEFAULT 事务隔离级别
timeout 不 -1 事务超时的时间(以秒为单位)
read-only 不 false 事务是否只读?
rollback-for 不 将被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException,ServletException'
no-rollback-for 不 不 被触发进行回滚的 Exception(s);以逗号分开。 如:'com.foo.MyBusinessException