根据慕课堂的spring事务管理的课程总结,链接地址:http://www.imooc.com/learn/478
spring事务管理分类
- 编程式事务
- 声明式事务
编程式事务
一般使用spring提供的TransactionTemplate作为事务的操作。
- spring 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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="20" />
<!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
<property name="filters" value="stat" />
</bean>
<!-- 使用编程式事务配置spring的事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 使用spring提供的TranscationTemplate编写编程式事务 -->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<bean id="accountDao" class="com.crk.demo1.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountService" class="com.crk.demo1.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao" />
<property name="transactionTemplate" ref="transactionTemplate" />
</bean>
</beans>
其中主要还是transactionManager和transactionTemplate的配置。
- service层的调用
/**
* 编程式事务的编写
*/
public void transfer(final String out, final String in, final double money) {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(
TransactionStatus paramTransactionStatus) {
accountDao.outMoney(out, money);
accountDao.inMoney(in, money);
}
});
}
编程式事务主要是调用了spring提供的TransactionTemplate模板操作事务,在execute()方法中需要提供TransactionCallback或者其实现类来操作真正需要事务控制的逻辑代码。
声明式事务
- 基于TransactionProxyFactoryBean的方式。
- 基于AspectJ的XML配置方式。
- 基于注解方式 。
基于TransactionProxyFactoryBean的方式。
- xml的配置
<!-- 使用声明式事务配置spring的事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置业务层的代理,使用TransactionProxyFactoryBean代理的方式声明事务 -->
<!-- 代理方式指定的事务控制需要为每一个目标对象都设置事务控制 -->
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置目标对象 -->
<property name="target" ref="accountService" />
<!-- 注入事务管理器 -->
<property name="transactionManager" ref="transactionManager"></property>
<!-- 注入事务的属性 -->
<property name="transactionAttributes">
<props>
<!--
prop的格式:
* PROPAGATION :事务的传播行为
* ISOTATION :事务的隔离级别
* readOnly :只读
* -EXCEPTION :发生哪些异常回滚事务
* +EXCEPTION :发生哪些异常不回滚事务
-->
<prop key="transfer">PROPAGATION_REQUIRED</prop>
<!-- <prop key="transfer">PROPAGATION_REQUIRED,readOnly</prop> -->
<!-- <prop key="transfer">PROPAGATION_REQUIRED,+java.lang.ArithmeticException</prop> -->
</props>
</property>
</bean>
- 测试方法
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext2.xml")
public class TestDemo2 {
@Resource(name="accountServiceProxy")
private IAccountService accountService;
@Test
public void test(){
accountService.transfer("aaa", "bbb", 200d);
}
}
这里主要是要注入的是代理accountServiceProxy类。
基于AspectJ的XML配置方式。
- xml的配置
<!-- 使用声明式事务配置spring的事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置事务的通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 通常的配置方式都是以update*或者insert*这样的通配符来对事务方法的事务控制 -->
<!-- 这里只有一个方法,就写上transfer -->
<!--
propagation :事务传播行为
isolation :事务的隔离级别
read-only :只读
rollback-for :发生哪些异常回滚
no-rollback-for :发生哪些异常不回滚
timeout :过期信息
-->
<tx:method name="transfer" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- aop配置事务的切面 -->
<aop:config>
<!-- 配置事务的切入点 -->
<!-- 定义在service包或者子包里的任意方法的执行
第一个* :方法值为任意
service.. :表示service包或者子包
第二个* :方法名为 任意
第三个* :参数为任意
-->
<aop:pointcut expression="execution(* com.crk.demo3.service..*.*(..))" id="pointcut1"/>
<!-- 配置事务的通知 -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut1"/>
</aop:config>
通过aop xml进行配置。具体的业务方法不用做任何修改。
基于注解方式
这种方式相对来说配置是最少的,但是需要在源代码的基础上添加注解。
- xml配置
<!-- 使用声明式事务配置spring的事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解事务的配置 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
- service实现类添加注解
/**
*@Transactional中的的属性
*propagation :事务的传播行为
*isolation :事务的隔离级别
*readOnly :只读
*rollbackFor :发生哪些异常回滚
*noRollbackFor :发生哪些异常不回滚
*rollbackForClassName :根据异常类名回滚
*timeout :超时时间
*transactionManager :配置具体的事务管理器
*/
@Transactional(propagation=Propagation.REQUIRED)
public class AccountServiceImpl implements IAccountService {}
总结
- 目前基本使用的较多的是声明式事务。
- 声明式事务中,使用的较多的是通过aop方式的xml配置和注解的方式。
- 基于AsceptJ的xml配置的优点是一旦配置好之后,类上不需要添加任何东西,对原来的代码不用做任务修改。
- 基于注解的方式的优点是配置的是最少的,但是需要在代码上添加上注解,需要修改原来的代码。