Spring学习(十一):声明式事务管理(响应式)

什么是事务:事务是数据库操作的最基本单元,逻辑上一组操作,要么都成功,如果有一个失败则意味着所有操作都失败

事务的特性(ACID):

1、原子性

2、一致性

3、隔离性

4、持久性        

假定一个场景,a给b转账100元,转出去的时候,突然出现错误,b并没有收到钱,这时需要数据回滚,使a的账户不要扣钱

1、创建UserDao接口

package demo.dao;

public interface UserDao {

    public void addMoney();
    public void reduceMoney();
}

2、创建UserDaoImpl类,继承userdao接口,实现里面的加钱和扣钱方法(其实一个方法就能实现),注入JdbcTemplate

package demo.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;


    /**
     * a给b转100
     */
    @Override
    public void addMoney() {

        String sql = "update t_account set money=money+? where username=?";
        jdbcTemplate.update(sql,100,"b");
    }

    @Override
    public void reduceMoney() {
        String sql = "update t_account set money=money-? where username=?";
        jdbcTemplate.update(sql,100,"a");
    }
}

3、创建配置文件bean1.xml,配置数据库连接池和JdbcTemplate,注入DataSource,创建事务管理器,开启组件扫描和事务注解

<?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.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">

<!--   开启组件扫描-->
    <context:component-scan base-package="demo"></context:component-scan>

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
          destroy-method="close">
        <property name="url" value="jdbc:mysql:///user_db" />
        <property name="username" value="root" />
        <property name="password" value="root" />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    </bean>

    <!-- jdbcTemplate对象 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!--        注入dataSource -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

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

<!--    开启事务注解-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>

</beans>

 

4、创建UserService类,注入dao,实现转账的业务操作,添加事务注解

package demo.service;

import demo.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional      //事务注解
public class UserService {

    //注入dao
    @Autowired
    private UserDao userDao;

    //转账
    public void accountMoney(){

        //a少100
        userDao.reduceMoney();

        //模拟异常
        int n = 10/0;

        //b多100
        userDao.addMoney();

    }
}

        进行一系列配置后,只要在类或者方法上加上事务注解,就可以实现事务操作

 

 5、根据需求建表

 

6、编写测试类测试

@Test
    public void testAccount(){
        ApplicationContext context = new ClassPathXmlApplicationContext("bean1.xml");
        UserService userService = context.getBean("userService", UserService.class);

        userService.accountMoney();

    }

控制台因为我们模拟的错误,已经报错了

但是数据并没有改变

为了验证我们代码的正确性,去掉模拟的错误,再次运行

可以看到转账成功

说明我们的代码确实可以完成事务操作。

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值