mybatis-plus 增加乐观锁

安装依赖 

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.5.2</version>
</dependency>

先配置好mybatis-plus开启乐观锁

/**
 * MyBatisPlus配置类
 */
@Configuration
@EnableTransactionManagement
public class MyBatisPlusConfig {

    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        // 配置分页
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        //配置乐观锁
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return interceptor;
    }

在你需要作为版本字段里面加入 @Version 注解

@Version
private Integer version;

写一个aop,进行添加失败重试处理(我是通过自定义注解来进行监听的) 例

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

@Aspect
@Component
public class RetryOnFailureAspect {
    //    private static final Logger logger = LoggerFactory.getLogger(RetryOnFailureAspect.class);
    private int maxRetries;

    public void setMaxRetries(int maxRetries) {
        this.maxRetries = maxRetries;
    }

    @AfterThrowing(pointcut = "@annotation(com.ruoyi.web.fzzn.common.note.NeedTryAgain)", throwing = "ex")
    public void retryOnFailure(JoinPoint joinPoint, Throwable ex) throws Throwable {
      if (ex instanceof TryAgainException){
            MethodSignature signature = (MethodSignature) joinPoint.getSignature();
            Method method = signature.getMethod();
            NeedTryAgain annotation = method.getAnnotation(NeedTryAgain.class);
            int maxAttempts = annotation.tryTimes();
            int attempts = 0;
            while (attempts < maxAttempts ) {
                attempts++;
                try {
                    method.invoke(joinPoint.getTarget(), joinPoint.getArgs());
                    System.out.println("---------------重试成功-------------------");
                    return;
                } catch (Throwable e) {
                    System.out.println("***********重试次数: "+attempts+"***********");
                }
            }
            throw new RuntimeException("重试失败");
        }
    }
}

自定义注解

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * 重试注解(异常重试,乐观锁更新失败重试)
 */
@Target(ElementType.METHOD) // 作用到方法上
@Retention(RetentionPolicy.RUNTIME)
public @interface NeedTryAgain {

    /**
     * 重试次数
     * @return
     */
    int tryTimes() default 3;

}

自定义一个异常  被乐观锁限制修改或新增失败时抛出这个异常 进行 重试 (可以在aop里面进行判断是否是自己抛出的这个异常)

import org.springframework.http.HttpStatus;

/**
 * 自定义 重试异常
 */
public class TryAgainException extends RuntimeException {

    private Integer status = 500;

    public TryAgainException( String msg) {
        super(msg);
    }

    public TryAgainException(HttpStatus status, String msg) {
        super(msg);
        this.status = status.value();
    }

}

controller示例   实体类和service自己重新定义  

Transactional注解应该加在service的方法上 这里为了方便 就没有进行展示了

 @PostMapping("save")
    @Transactional(noRollbackFor = {TryAgainException.class})
    @NeedTryAgain()
    public R save(@RequestBody ArithmeticEntity arithmeticEntity) {
        ArithmeticEntity byId = arithmeticService.getById(arithmeticEntity.getId());
        ArithmeticEntity byId1 = arithmeticService.getOne(new LambdaQueryWrapper<ArithmeticEntity>().eq(ArithmeticEntity::getId, arithmeticEntity.getId()));
        byId.setSubscription(byId.getSubscription() + CommonConstants.size);
        byId1.setSubscription(byId1.getSubscription() + CommonConstants.size);
        boolean b1 = arithmeticService.updateById(byId1);
        if (!b1) {
//          回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            // 保存失败,抛出重试异常
            throw new TryAgainException("乐观锁导致修改失败");
        }
        ArithmeticUserEntity arithmeticUser = new ArithmeticUserEntity();
        BeanUtils.copyProperties(byId, arithmeticUser, "id");
        arithmeticUser.setBelongType(CommonConstants.size);
        arithmeticUserService.save(arithmeticUser);
        return R.ok();
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值