前言:事务管理有两种方式,一种是编程方式,另外一种是配置的方式。本文介绍的是第二种。
第二种又有两种,一是基于XML的配置,二是注解方式的。
我们还是在Spring的JDBC模板使用之三--对数据表进行CURD的操作 这篇案例的基础上演示。
一、基于XML的配置
1.在IAccountDao接口中添加一个方法transfer
//模拟转账:outUser转出名,inUsesr,转入名,money:转账金额
void transfer(String outUser,String inUser,Double money);
2.AccountDao的实现方法:
(1)模拟没有出现异常
@Override
/**
* 转账
* inUser:收款人
* outUser:汇款人
* money:转账金额
*/
public void transfer(String outUser, String inUser, Double money) {
//收款时
sql="update account set balance = balance+? where username=?";
this.jdbcTemplate.update(sql,money,inUser);
//汇款时
sql="update account set balance = balance-? where username=?";
this.jdbcTemplate.update(sql,money,outUser);
}
测试代码:
@Test
public void testTransfer() {
accountDao.transfer("胡兰", "刘备", 100d);
}
测试前:
测试后:
(2)模拟出现异常情况,在transfer方法中,添加一句int i=1/0; 模拟异常
a.没有配置事务前,测试结果
b.配置了事务后
以上测试证明:配置了事务管理后,运行结果满足业务需求。
配置代码如下:
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置数据源 -->
<!-- <bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///spring?useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean> -->
<!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置VO -->
<bean id="account" class="cn.zdxh.jdbc.demo2.Account"/>
<!-- 配置Dao -->
<bean id="accountDao" class="cn.zdxh.jdbc.demo2.AccountDao">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 配置事务管理器,依赖于数据源 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 编写通知:对事务进行增强,需要编写好切入点和具体执行事务细节
项目中要导入aop基于aspectj开发的jar包,配置文件需要引入aop和tx名称空间 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- *表示任何方法 -->
<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面 -->
<aop:config>
<aop:pointcut expression="execution(* cn.zdxh.jdbc.demo2.*.*(..))" id="txPointCut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
</beans>
二、基于注解的事务管理
1.配置文件关于事务管理的,只需要如下图配置
配置文件源码
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 配置VO -->
<bean id="account" class="cn.zdxh.tx.annotation.Account"/>
<!-- 配置Dao -->
<bean id="accountDao" class="cn.zdxh.tx.annotation.AccountDao">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!-- 配置事务管理器,依赖于数据源 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 注册事务管理器驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
2.在AccountDao的transfter方法所在类前增加@Transactional注解,表示该类的所有方法都使用事务的设置,如果该注解用在某个方法上面,则该事务设置只对该方法有效。括号里面的属性,如果是默认值,可以不写。
3.测试
测试代码
package cn.zdxh.tx.annotation;
import static org.junit.Assert.*;
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;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:cn/zdxh/tx/annotation/bean3.xml")
public class AccountAnnotationTest {
@Resource(name="accountDao")
private IAccountDao accountDao;
@Test
public void testTransfer() {
accountDao.transfer("alice", "tom", 500d);
}
}
(1)无事务注解
测试前:
测试后:
(2)有事务注解
测试前:
测试后:
上面使用两种方法演示了Spring的事务管理,两个方法都要掌握。