用事务通知声明式地管理事务
事务管理是一种横切关注点。为了在 Spring 2.x 中启用声明式事务管理,可以通过 tx Schema 中定义的 <tx:advice> 元素声明事务通知,为此必须事先将这个 Schema 定义添加到 <beans> 根元素中去。声明了事务通知后,就需要将它与切入点关联起来。由于事务通知是在 <aop:config> 元素外部声明的,所以它无法直接与切入点产生关联。所以必须在 <aop:config> 元素中声明一个增强器通知与切入点关联起来。由于 Spring AOP 是基于代理的方法,所以只能增强公共方法。因此,只有公有方法才能通过 Spring 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: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/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
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-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<!-- 导入资源文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置 C3P0 数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="initialPoolSize" value="${c3p0.initialPoolSize}"></property>
<property name="minPoolSize" value="${c3p0.minPoolSize}"></property>
<property name="maxPoolSize" value="${c3p0.maxPoolSize}"></property>
<property name="maxIdleTime" value="${c3p0.maxIdleTime}"></property>
<property name="idleConnectionTestPeriod" value="${c3p0.idleConnectionTestPeriod}"></property>
</bean>
<!-- 配置 NamedParameterJdbcTemplate, 该对象可以使用具名参数, 其没有无参数的构造器, 所以必须为其构造器指定参数 -->
<bean id="namedParameterJdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"></constructor-arg>
</bean>
<bean id="salaryDao" class="xyz.huning.spring4.transaction.xml.dao.impl.SalaryDao">
<property name="namedParameterJdbcTemplate" ref="namedParameterJdbcTemplate"></property>
</bean>
<bean id="salaryService" class="xyz.huning.spring4.transaction.xml.service.impl.SalaryService">
<property name="salaryDao" ref="salaryDao"></property>
</bean>
<bean id="batchSalaryService" class="xyz.huning.spring4.transaction.xml.service.impl.BatchSalaryService">
<property name="salaryService" ref="salaryService"></property>
</bean>
<!-- 1. 配置事务管理器 -->
<bean id="transactionMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 2. 配置事务通知 -->
<tx:advice id="salaryServiceTx" transaction-manager="transactionMgr">
<tx:attributes>
<!-- 根据方法名指定事务的属性 -->
<tx:method name="payoff" propagation="REQUIRES_NEW"/>
<tx:method name="*"/><!--问题:如果不配置tx:method节点,事务异常时不回滚,不知道为什么?-->
</tx:attributes>
</tx:advice>
<!-- 3. 配置事务通知绑定通知方法(即需要进行事务管理的方法):
3.1 配置事务切入点;
3.2 绑定事务切入点和事务通知
-->
<aop:config>
<aop:pointcut id="salaryServicePt" expression="execution(* xyz.huning.spring4.transaction.xml.service.impl.*.*(..))"/>
<aop:advisor advice-ref="salaryServiceTx" pointcut-ref="salaryServicePt"/>
</aop:config>
</beans>
问题:
1. 如果从上文中删除下面的配置内容,系统发生异常后不会回滚,为什么?
<tx:attributes> <!-- 根据方法名指定事务的属性 --> <tx:method name="payoff" propagation="REQUIRES_NEW"/> <tx:method name="*"/><!--问题:如果不配置tx:method节点,事务异常时不回滚,不知道为什么?--> </tx:attributes>
2. 同一个类中嵌套事务,被调用的事务设置传播行为为Propagation.REQUIRES_NEW时不生效,发生异常时依然是全部回滚。为什么?
3564

被折叠的 条评论
为什么被折叠?



