01-spring-aop@Before-对事务处理-例子-你我寿命的转移-有分析&代码(账户转账操作)

测试前准备:

这里就用我之前的blog数据库了哇,主要懒得在添加表了,将就着叭,所以就用用户寿命年龄代替银行转账事务的处理【手动滑稽】

在这里插入图片描述

core-code:核心代码:

public void transfer(Integer source_user_id, Integer target_user_id, int age_money) {
    // 1. the source id;
    blogUser source = iBlogUserDao.findById(source_user_id);
    // 2. the target id;
    blogUser target = iBlogUserDao.findById(target_user_id);
    // 3. the source move the age;
    source.setUser_age(source.getUser_age()-age_money);
    // 4. the target fetch the source age;
    target.setUser_age(target.getUser_age()+age_money);
    // 5. update the source info;
    iBlogUserDao.update_user(source);
    // +: the throwing trouble:
    // int psycho = 1/0; // psycho-to-神经病;
    
    // 6. update the target info;
    iBlogUserDao.update_user(target);
}

案例,实现用户寿命年龄的转移;(事务)

先看效果:

转移前:

在这里插入图片描述


操作事务:

在这里插入图片描述

as follows:

在这里插入图片描述

他的功能实现impl:

在这里插入图片描述


然后试着添加一个中断错误:重新操作:

在这里插入图片描述

就抛出异常了吧:ArithmeticException: / by zero
在这里插入图片描述


分析:

我印象里就晓得什么commit和rollback咯,大概就是要让这个转账操作原子性;

  • 打开事务,
  • 执行操作;
  • 提交事务|| 或者, 发生异常,回滚事务操作;
  • 返回结果;
  • 释放资源;

引入THreadLocal(<ThreadLocal作用、场景、原理 - 简书 (jianshu.com)>):

有两个工具类:

// tech.jasonch.utils.connectionUtils.java
package tech.jasonch.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
// dao
@Repository("connUtils")
public class connectionUtils {
    private ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
    @Autowired
    private DataSource dataSource;

//    public void  setDataSource(DataSource dataSource) {
//        this.dataSource = dataSource;
//    }//

    public Connection getThreadConnection(){
        // ctrl+alt+t: make the try/catch block;
        // 1.
        try {
            Connection conn = threadLocal.get();
            // 2. isNull
            if(conn==null){
                conn = dataSource.getConnection();
                threadLocal.set(conn);
            }
            return conn;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    public void removeConnection(){
        threadLocal.remove();
    }
}
// tech.jasonch.utils.transactionManager.java;
package tech.jasonch.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;

@Repository
public class transactionManager {
    @Autowired
    private connectionUtils connUtils;

//    public void setConnUtils(connectionUtils connUtils) {
//        this.connUtils = connUtils;
//    }

    public void beginTransaction(){
        try {
            connUtils.getThreadConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void commit(){
        try {
            connUtils.getThreadConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void rollback(){
        try {
            System.out.println("噢,很抱歉,发生了异常,事务操作终止噢~");
            connUtils.getThreadConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void release(){
        try {
            // reBack to the conn-pool;
            connUtils.getThreadConnection().close();
            connUtils.removeConnection();// 解绑;
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

业务实现方法:

/**
 * commit & rollback// the package: tech.jasonch.service.impl.userServiceImpl.java;
 * @param source_user_id
 * @param target_user_id
 * @param age_money
 */
public void transfer(Integer source_user_id, Integer target_user_id, int age_money) {
    try {
        //1. open the transaction
        txManager.beginTransaction();
        //2. go the func;
        //
            // 1. the source id;
            blogUser source = iBlogUserDao.findById(source_user_id);
            // 2. the target id;
            blogUser target = iBlogUserDao.findById(target_user_id);
            // 3. the source move the age;
            source.setUser_age(source.getUser_age()-age_money);
            // 4. the target fetch the source age;
            target.setUser_age(target.getUser_age()+age_money);
            // 5. update the source info;
            iBlogUserDao.update_user(source);
            // +: the throwing trouble:
            int psycho = 1/0; // psycho-to-神经病;// here is one err may stop the transaction;
            // 6. update the target info;
            iBlogUserDao.update_user(target);
        //
        //3. commit the transaction;
        txManager.commit();
        //4. return the result;
    } catch (Exception e) {
        // rollback
        txManager.rollback();
    } finally {
        // release;
        txManager.release();
    }
}

执行操作后:

在这里插入图片描述

说明事务处理成功啦here;再看看数据库中年龄:在这里插入图片描述

也没有发生变化,successfully over;


总结:

在实现方法中:这里只展示了转账方法,还有很多业务操作需要加入事务操作的过程代码,也就大大增加了代码的后期维护修改困难程度;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

J.CH.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值