Spring事务管理

52 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

搭建转账业务环境

1. 创建数据库表account

图片1.png

2. 创建AccountDao接口

public interface AccountDao {
    //转出
    public void outMoney(String to,Double money);
    //转入
    public void inMoney(String from,Double money);
}

3. 实现AccountDao接口得到AccountDaoImpl

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao {
   /* //属性注入的方式获取jdbcTemplate
      //也可以通过继承 JdbcDaoSupport获取jdbcTemplate
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }*/
    //转出
    @Override
    public void outMoney(String name, Double money) {
        this.getJdbcTemplate().update("update account set money = money-? where name=?", money, name);
    }
    //转入
    @Override
    public void inMoney(String name, Double money) {
        this.getJdbcTemplate().update("update account set money = money+? where name=?", money, name);
    }
}

4. 将创建转账业务接口

public interface AccountService {
    public void transferMoney(String from,String to,Double money);
}

5. 实现转账业务接口得到AccountServiceImpl

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }
    @Override
    public void transferMoney(String from, String to, Double money) {
        accountDao.outMoney(from, money);
        accountDao.inMoney(to,money);
    }
}

6. 编写属性文件property.property

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///Spring
user=root
password=123456

7. 将AccountDaoImpl与AccountServiceImpl交给Spring管理

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--开启IOC注解-->
    <context:annotation-config/>
    <!--开启AOP注解-->
    <aop:aspectj-autoproxy/>
    <!--配置文件配置Bean-->
    <!--加载属性文件-->
    <context:property-placeholder location="property"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--注入属性,引用属性名称的key值-->
        <property name="driverClassName" value="${driverClass}"/>
        <property name="Url" value="${jdbcUrl}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
    </bean>
    <!--JdbcTemplate注入连接池-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置accountDaoImpl,并注入数据源-->
    <bean id="accountDao" class="com.ph.demo3.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置Service,并注入accountDao-->
    <bean id="accountService" class="com.ph.demo3.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>
</beans>

8. 编写测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class test {
    @Resource
    private AccountService accountService;
    @Test
    public void testTransfer() {
        accountService.transferMoney("张三", "李四", 100.00);
        System.out.println("转账成功");
    }
}

运行结果

图片2.png

图片3.png

      此时我们可以看到,转账成功后,张三的账户余额少了100,而李四的账户余额多了100,这是没有问题的。但是如果在转账的过程出现了错误,导致了一方转出,而另一方没有收到转入,那么就是一个问题了。我们可以模拟一下:我们将转账业务注入一个异常

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transferMoney(String from, String to, Double money) {
        accountDao.outMoney(from, money);
        int i=10/0;//注入异常
        accountDao.inMoney(to,money);
    }
}

再次运行测试类

图片4.png

图片5.png

      我们可以看到,张三扣减了100,但是李四并没有增加100。这是非常致命的缺陷,因此我们需要做一个事务管理来解决这个问题,事务保证要么不做,要么全做,不会出现一方扣减,另一方步增加的情况。事务的4个属性(ACID)原子性、一致性、隔离性、持久性,如果还不清楚的可以重新去看一下数据库相关的书籍

添加事务

1. 编程式事务

配置平台事务管理器


<bean id **="transactionManager" **class **="org.springframework.jdbc.datasource.DataSourceTransactionManager" **>
<property name **="dataSource" **ref **="dataSource" **/>
</bean>

配置Spring提供的事务管理的模板类


<bean id **="transactionTemplate" **class **="org.springframework.transaction.support.TransactionTemplate" **>
<property name **="transactionManager" **ref **="transactionManager" **/>
</bean>

在业务bean中注入事务管理的模板


<bean id **="accountService" **class **="com.ph.demo3.AccountServiceImpl" **>
<property name **="accountDao" **ref **="accountDao" **/>
<property name **="transactionTemplate" **ref **="transactionTemplate" **/>
</bean>

最终的applicationContext.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
    <!--开启IOC注解-->
    <context:annotation-config/>
    <!--开启AOP注解-->
    <aop:aspectj-autoproxy/>
    <!--配置文件配置Bean-->
    <!--加载属性文件-->
    <context:property-placeholder location="property"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--注入属性,引用属性名称的key值-->
        <property name="driverClassName" value="${driverClass}"/>
        <property name="Url" value="${jdbcUrl}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
    </bean>
    <!--JdbcTemplate注入连接池-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务管理模板-->
    <bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
        <property name="transactionManager" ref="transactionManager"/>
    </bean>
    <!--配置accountDaoImpl,并注入数据源-->
    <bean id="accountDao" class="com.ph.demo3.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置Service,并注入accountDao和事务管理模板-->
    <bean id="accountService" class="com.ph.demo3.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
        <property name="transactionTemplate" ref="transactionTemplate"/>
    </bean>
</beans>

编写事务管理的代码

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;
    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    private TransactionTemplate transactionTemplate;
    public void setTransactionTemplate(org.springframework.transaction.support.TransactionTemplate transactionTemplate) {
        this.transactionTemplate = transactionTemplate;
    }

    @Override
    public void transferMoney(String from, String to, Double money) {
        //使用事务管理
        this.transactionTemplate.execute(new TransactionCallbackWithoutResult() {
            @Override
            protected void doInTransactionWithoutResult(TransactionStatus transactionStatus) {
                accountDao.outMoney(from, money);
//        int i=10/0;//注入异常
                accountDao.inMoney(to,money);
            }
        });
    }
}

2. 声明式事务

2.1  XML方式声明事务管理

2.1.1 引入aop的开发包

2.1.2 配置事务管理器


<bean id **="transactionManager" **class **="org.springframework.jdbc.datasource.DataSourceTransactionManager" **>
<property name **="dataSource" **ref **="dataSource" **/>
</bean>

2.1.3 AOP的配置


<tx :advice id **="txAdvice" **transaction-manager **="transactionManager" **>
<tx :attributes>
<tx :method name *="" **propagation **="REQUIRED" **/>
</tx :attributes>
</tx :advice>

<aop :config >
<aop :pointcut id **="pointcut" **expression ="execution( com.ph.demo3.AccountServiceImpl.(..))" **/>
<aop :advisor advice-ref **="txAdvice" **pointcut-ref **="pointcut" **/>
</aop :config>

最终的applicationContext.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
       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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--开启IOC注解-->
    <context:annotation-config/>
    <!--开启AOP注解-->
    <aop:aspectj-autoproxy/>
    <!--配置文件配置Bean-->
    <!--加载属性文件-->
    <context:property-placeholder location="property"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--注入属性,引用属性名称的key值-->
        <property name="driverClassName" value="${driverClass}"/>
        <property name="Url" value="${jdbcUrl}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
    </bean>
    <!--JdbcTemplate注入连接池-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置增强-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--配置AOP-->
    <aop:config >
        <aop:pointcut id="pointcut" expression="execution(* com.ph.demo3.AccountServiceImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>
    <!--配置accountDaoImpl,并注入数据源-->
    <bean id="accountDao" class="com.ph.demo3.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置Service,并注入accountDao-->
    <bean id="accountService" class="com.ph.demo3.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>
</beans>

2.1.4 编写事务管理的代码

public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transferMoney(String from, String to, Double money) {
        //使用事务管理
        accountDao.outMoney(from, money);
       //int i = 10 / 0;//注入异常
        accountDao.inMoney(to, money);
    }
}

2.2  注解式声明事务管理

2.2.1 配置事务管理器


<bean id **="transactionManager" **class **="org.springframework.jdbc.datasource.DataSourceTransactionManager" **>
<property name **="dataSource" **ref **="dataSource" **/>
</bean>

2.2.2 开启注解事务


<tx :annotation-driven transaction-manager **="transactionManager" **/>

最终的applicationContext.xml文件如下

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cache="http://www.springframework.org/schema/cache"
       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.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--开启IOC注解-->
    <context:annotation-config/>
    <!--开启AOP注解-->
    <aop:aspectj-autoproxy/>
    <!--配置文件配置Bean-->
    <!--加载属性文件-->
    <context:property-placeholder location="property"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!--注入属性,引用属性名称的key值-->
        <property name="driverClassName" value="${driverClass}"/>
        <property name="Url" value="${jdbcUrl}"/>
        <property name="username" value="${user}"/>
        <property name="password" value="${password}"/>
    </bean>
    <!--JdbcTemplate注入连接池-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--开启注解事务-->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    <!--配置accountDaoImpl,并注入数据源-->
    <bean id="accountDao" class="com.ph.demo3.AccountDaoImpl">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置Service,并注入accountDao-->
    <bean id="accountService" class="com.ph.demo3.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>
</beans>

2.2.3 在业务代码类上添加注解@Transactional

@Transactional
public class AccountServiceImpl implements AccountService {
    private AccountDao accountDao;

    public void setAccountDao(AccountDao accountDao) {
        this.accountDao = accountDao;
    }

    @Override
    public void transferMoney(String from, String to, Double money) {
        //使用事务管理
        accountDao.outMoney(from, money);
       int i = 10 / 0;//注入异常
        accountDao.inMoney(to, money);
    }
}