java - 保存日志到mongo失败,乐观锁异常
黄舟
黄舟 2017-04-18 10:51:27
0
1
1145

想把mongo保存进mongoDB中,使用的spring-boot-data-mongo.
在dao层可以正常保存,但是写在service中报错:

org.springframework.dao.OptimisticLockingFailureException: Optimistic lock exception on saving entity: {_class=com.zihexin.base.mongo.MerchantRequestLog, version=1, requestIp=1111, requestDate=Tue Mar 21 17:15:01 CST 2017, requestParam=22222, paymentWay=213} to collection merchant_request_log

    at org.springframework.data.mongodb.core.MongoTemplate$12.doInCollection(MongoTemplate.java:1135)
    at org.springframework.data.mongodb.core.MongoTemplate$12.doInCollection(MongoTemplate.java:1108)
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:461)
    at org.springframework.data.mongodb.core.MongoTemplate.doUpdate(MongoTemplate.java:1108)
    at org.springframework.data.mongodb.core.MongoTemplate.doSaveVersioned(MongoTemplate.java:977)
    at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:941)
    at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.save(SimpleMongoRepository.java:78)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:483)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:468)
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:440)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    at com.sun.proxy.$Proxy84.save(Unknown Source)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    at com.sun.proxy.$Proxy84.save(Unknown Source)
    at com.zihexin.base.service.MongoService.saveRequestLog(MongoService.java:44)
    at com.zihexin.ZhxpayCommonApplicationTests.test4(ZhxpayCommonApplicationTests.java:72)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

service中代码:

@Service
public class MongoService {

    private static Logger logger = LoggerFactory.getLogger(MongoService.class);

    /**商户请求相关*/
    //判断流水号重复或者为空
    @Autowired
    MerchantRequestLogRepo merchantRequestLogRepo;

    public boolean exists(String requestId){
        return merchantRequestLogRepo.exists(requestId);
    }
    //异步保存mongo日志
//    @Async
    public void saveRequestLog(String ip,String paramMap, String requestId,String paymentWay){

        long start = System.currentTimeMillis();
        logger.info("Async Save Merchant Request Log Begin : ");

        MerchantRequestLog merchantRequestLog = new MerchantRequestLog();
        merchantRequestLog.setRequestDate(new Date());
        merchantRequestLog.setRequestId(requestId);
        merchantRequestLog.setRequestParam(paramMap);
        merchantRequestLog.setRequestIp(ip);
        merchantRequestLog.setPaymentWay(paymentWay);
//        merchantRequestLog.setHostIp(Constants.hostAddress);
        merchantRequestLogRepo.save(merchantRequestLog);
        logger.info("Async Save Merchant Request Log End ,Spend Time :{} ms",System.currentTimeMillis()-start);
    }

save()方法为继承MongoRepository中的方法,我改为insert()后就可以正常保存了,想不明白为什么,希望大家帮忙解答一下,谢谢!

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

모든 응답(1)
巴扎黑

고려 사항 2가지:

1. 낙관적 잠금은 주로 버전에 의존하여 충돌을 처리합니다.

으아악

2. 저장은 안 되는데 삽입은 왜 되나요? MongoDB에서 저장과 삽입의 차이점에 유의하세요.

으아악

참고로.

MongoDB를 사랑해주세요! 재미있게 보내세요!

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿