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

  • 搭建转账业务环境
  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。这是非常致命的缺陷,因此我们需要做一个事务管理来解决这个问题,事务保证要么不做,要么全做&#x

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值