第一种方式:使用TransactionTemplate模板
步骤
- 配置事务核心管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
- 配置使用TransactionTemplate模板
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
- 然后在service方法中配置transactionTemplate模板且使用execute方法
<bean id="accountServiceImpl" class="com.service.impl.AccountServiceImpl">
<property name="ac" ref="accountDaoImpl"></property>
<property name="tt" ref="transactionTemplate"></property>
</bean>
service方法
- 使用execute方法,传入TransactionCallbackWithoutResult对象
- execute的底层执行流程
- 先打开事务
- 调用doInTransactionWithoutResult方法
- 提交事务
- 有try/catch执行,有异常回滚
public class AccountServiceImpl implements IAccountService{
private AccountDaoImpl ac;
private TransactionTemplate tt;
@Override
public void transfer(final Integer from,final Integer to,final Double money) {
/**
* execute的底层执行流程
* 1. 先打开事务
* 2. 调用doInTransactionWithoutResult方法
* 3. 提交事务
* 4. 有try/catch执行,有异常回滚
*/
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
ac.addMoney(to, money);
ac.decreseMoney(from, money);
}
});
}
public void setAc(AccountDaoImpl ac) {
this.ac = ac;
}
public void setTt(TransactionTemplate tt) {
this.tt = tt;
}
}
总配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
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 ">
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="accountServiceImpl" class="com.service.impl.AccountServiceImpl">
<property name="ac" ref="accountDaoImpl"></property>
<property name="tt" ref="transactionTemplate"></property>
</bean>
<bean id="accountDaoImpl" class="com.dao.impl.AccountDaoImpl">
<property name="jt" ref="jdbcTemplate"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
</beans>
dao类
public class AccountDaoImpl implements IAccountDao{
private JdbcTemplate jt;
@Override
public void addMoney(Integer id, Double money) {
String sql= "update t_user set t_money = t_money + ? where t_id = ?";
jt.update(sql,money,id);
}
@Override
public void decreseMoney(Integer id, Double money) {
String sql= "update t_user set t_money = t_money - ? where t_id = ?";
jt.update(sql,money,id);
}
public void setJt(JdbcTemplate jt) {
this.jt = jt;
}
}
测试类
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class TestDemo {
@Resource(name="accountServiceImpl")
private IAccountService as;
@Test
public void test1(){
as.transfer(1, 2, 100d);
}
}