安装依赖
com.baomidou
mybatis-plus-boot-starter
3.5.2
先配置好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().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();
}