spring声明式事务管理,分为三种方式,分别是:
1、使用代理类TransactionProxyFactoryBean的方式
2、使用aspectj+xml配置的方式
3、使用事务注解的方式
其中后面两种是比较常用的,使用xml和注解的方式。
这里介绍使用代理类TransactionProxyFactoryBean的方式:
pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
</dependencies>
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///shop?useUnicode=true&useSSL=false
jdbc.username=hadoop
jdbc.password=hadoop
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: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.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="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<bean id="accountDao" class="com.xxx.springtransaction.dao.impl.AccountDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountService"
class="com.xxx.springtransaction.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- spring transaction proxy -->
<bean id="accountServiceProxy"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager" ref="transactionManager" />
<property name="target" ref="accountService" />
<property name="transactionAttributes">
<props>
<prop key="transfer">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
</beans>
AccountDao.java
package com.xxx.springtransaction.dao;
public interface AccountDao {
public void transferIn(String id,Double money);
public void transferOut(String id,Double money);
public Double findById(String id);
}
AccountDaoImpl.java
package com.xxx.springtransaction.dao.impl;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.xxx.springtransaction.dao.AccountDao;
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
@Override
public void transferIn(String id, Double money) {
String sql = "update account set money = money + ? where id = ?";
getJdbcTemplate().update(sql, money,id);
}
@Override
public void transferOut(String id, Double money) {
String sql = "update account set money = money - ? where id = ?";
getJdbcTemplate().update(sql, money,id);
}
@Override
public Double findById(String id){
return null;
}
}
AccountService.java
package com.xxx.springtransaction.service;
public interface AccountService {
public void transfer(String out,String in,Double money);
public Double findById(String id);
}
AccountServiceImpl.java
package com.xxx.springtransaction.service.impl;
import com.xxx.springtransaction.dao.AccountDao;
import com.xxx.springtransaction.service.AccountService;
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void transfer(String out, String in, Double money) {
accountDao.transferOut(out, money);
int i = 1/0;
System.out.println(i);
accountDao.transferIn(in, money);
}
@Override
public Double findById(String id){
return accountDao.findById(id);
}
}
单元测试类:SpringTransactionTest.java
package com.xxx.springtransaction;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.xxx.springtransaction.service.AccountService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:spring.xml")
public class SpringTransactionTest {
@Resource(name="accountServiceProxy")
private AccountService accountService;
@Test
public void demo1(){
accountService.transfer("1", "2", 200d);
}
}
代理方式事务管理的重点在于xml配置里面,需要为target为accountService的目标类配置事务属性。
在调用业务层方法时,需要引入的对象已经不是accountService了,而是变成了代理类accountServiceProxy。
测试事务生效的方法:
1、默认不使用事务,单元测试方法中还是使用@Resource(name="accountService"),并且注释掉业务层方法的异常代码:
int i = 1/0;
System.out.println(i);
aaa向bbb转账200元成功,aaa的账户变为800,bbb的账户变为1200。
2、去掉业务层的异常代码注释,这时候不使用事务,aaa转账bbb200元,aaa的账户变为600元,但是因为异常,bbb的账户并不会增加200元,最终aaa账户600,bbb的账户1200元,钱转丢了。
3、使用事务,单元测试类中@Resource(name="accountServiceProxy"),并且去掉业务层异常代码的注释。aaa再次给bbb转账,这时候因为异常,而且使用了事务。aaa的账户不会减少200元,而且bbb的账户也不会增加200元。在测试2的基础上aaa的账户还是600元,bbb的账户还是1200元。