Commit e4e73cce authored by 杨锐's avatar 杨锐

redis分布式锁,处理/user/center/save/userExtInfo并发问题,导致MySQLIntegrityConstraintViolationException

parent 8d88ed7c
...@@ -2,6 +2,7 @@ package cn.quantgroup.xyqb.aspect.lock; ...@@ -2,6 +2,7 @@ package cn.quantgroup.xyqb.aspect.lock;
import cn.quantgroup.xyqb.exception.ResubmissionException; import cn.quantgroup.xyqb.exception.ResubmissionException;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
...@@ -9,8 +10,6 @@ import org.aspectj.lang.annotation.Around; ...@@ -9,8 +10,6 @@ import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut; import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature; import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
...@@ -24,12 +23,11 @@ import java.lang.reflect.Method; ...@@ -24,12 +23,11 @@ import java.lang.reflect.Method;
/** /**
* Redis Lock Aspect * Redis Lock Aspect
*/ */
@Slf4j
@Aspect @Aspect
@Component @Component
public class RedisLockAspect { public class RedisLockAspect {
private final static Logger LOGGER = LoggerFactory.getLogger(RedisLockAspect.class);
@Autowired @Autowired
@Qualifier("stringRedisTemplate") @Qualifier("stringRedisTemplate")
private RedisTemplate<String, String> stringRedisTemplate; private RedisTemplate<String, String> stringRedisTemplate;
...@@ -70,7 +68,7 @@ public class RedisLockAspect { ...@@ -70,7 +68,7 @@ public class RedisLockAspect {
lockKey = prefix.concat(":").concat(keySPEL); lockKey = prefix.concat(":").concat(keySPEL);
} }
} catch (ExpressionException e) { } catch (ExpressionException e) {
LOGGER.error("key表达式“" + keySPEL + "”错误:{}", e); log.error("key表达式“" + keySPEL + "”错误:{}", e);
throw e; throw e;
} }
} else { } else {
...@@ -85,11 +83,11 @@ public class RedisLockAspect { ...@@ -85,11 +83,11 @@ public class RedisLockAspect {
if (lock.lock()) { if (lock.lock()) {
return pjp.proceed(); return pjp.proceed();
} else { } else {
LOGGER.warn("调用方法失败,已有业务数据在处理中 lockKey:{}", lock.getLockKey()); log.warn("调用方法失败,已有业务数据在处理中 lockKey:{}", lock.getLockKey());
throw new ResubmissionException(); throw new ResubmissionException();
} }
} catch (InterruptedException e) { } catch (InterruptedException e) {
LOGGER.warn("获取锁失败:lockKey:{},exception:{}", lock.getLockKey(), e.getMessage()); log.warn("获取锁失败:lockKey:{},exception:{}", lock.getLockKey(), e.getMessage());
throw new ResubmissionException(); throw new ResubmissionException();
} finally { } finally {
lock.unlock(); lock.unlock();
......
...@@ -171,6 +171,6 @@ public class ExceptionHandlingController implements IBaseController { ...@@ -171,6 +171,6 @@ public class ExceptionHandlingController implements IBaseController {
@ResponseStatus(HttpStatus.OK) @ResponseStatus(HttpStatus.OK)
@ResponseBody @ResponseBody
public JsonResult resubmissionException(ResubmissionException re) { public JsonResult resubmissionException(ResubmissionException re) {
return new JsonResult(re.getMessage(), 0L, null, re.getBusinessCode()); return JsonResult.buildErrorStateResult(re.getMessage(), null);
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment