从零开始学Spring(十三)——Spring事务管理

本文从一个转账业务场景出发,探讨了在出现异常时事务管理的重要性。介绍了Spring的编程式和声明式事务管理,包括DataSourceTransactionManager的配置、TransactionTemplate的使用,以及XML和注解方式声明事务管理的步骤。通过事务管理,确保了业务操作的原子性和一致性。
  • 搭建转账业务环境
  1. 创建数据库表account

  1. 创建AccountDao接口
public interface AccountDao {
    //转出
    public void outMoney(String to,Double money);
    //转入
    public void inMoney(String from,Double money);
}
  1. 实现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);
    }
}
  1. 将创建转账业务接口
public interface AccountService {
    public void transferMoney(String from,String to,Double money);
}
  1. 实现转账业务接口得到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);
    }
}
  1. 编写属性文件property.property

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

  1. 将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>
  1. 编写测试类
@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("转账成功");
    }
}

运行结果

      此时我们可以看到,转账成功后,张三的账户余额少了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);
    }
}

再次运行测试类

 

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

  • 添加事务
  1. 编程式事务
    1. 配置平台事务管理器

<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

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

<!--配置事务管理模板-->
<bean id="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
    <property name="transactionManager" ref="transactionManager"/>
</bean>

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

<!--配置Service,并注入accountDao和事务管理模板-->
<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>
    1. 编写事务管理的代码
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);
            }
        });
    }
}
  1. 声明式事务

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-->
<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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值