保存日志到mongo失败,乐观锁异常
澳门新葡京官网
澳门新葡京官网
当前位置 : 澳门新葡京官网 > 新葡京娱乐场

保存日志到mongo失败,乐观锁异常

想把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.doInCollectionMongoTemplate.java:1135
    at org.springframework.data.mongodb.core.MongoTemplate$12.doInCollectionMongoTemplate.java:1108
    at org.springframework.data.mongodb.core.MongoTemplate.executeMongoTemplate.java:461
    at org.springframework.data.mongodb.core.MongoTemplate.doUpdateMongoTemplate.java:1108
    at org.springframework.data.mongodb.core.MongoTemplate.doSaveVersionedMongoTemplate.java:977
    at org.springframework.data.mongodb.core.MongoTemplate.saveMongoTemplate.java:941
    at org.springframework.data.mongodb.repository.support.SimpleMongoRepository.saveSimpleMongoRepository.java:78
    at sun.reflect.NativeMethodAccessorImpl.invoke0Native Method
    at sun.reflect.NativeMethodAccessorImpl.invokeNativeMethodAccessorImpl.java:62
    at sun.reflect.DelegatingMethodAccessorImpl.invokeDelegatingMethodAccessorImpl.java:43
    at java.lang.reflect.Method.invokeMethod.java:498
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOnRepositoryFactorySupport.java:483
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvokeRepositoryFactorySupport.java:468
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invokeRepositoryFactorySupport.java:440
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceedReflectiveMethodInvocation.java:179
    at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invokeDefaultMethodInvokingMethodInterceptor.java:61
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceedReflectiveMethodInvocation.java:179
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invokeExposeInvocationInterceptor.java:92
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceedReflectiveMethodInvocation.java:179
    at org.springframework.aop.framework.JdkDynamicAopProxy.invokeJdkDynamicAopProxy.java:208
    at com.sun.proxy.$Proxy84.saveUnknown Source
    at sun.reflect.NativeMethodAccessorImpl.invoke0Native Method
    at sun.reflect.NativeMethodAccessorImpl.invokeNativeMethodAccessorImpl.java:62
    at sun.reflect.DelegatingMethodAccessorImpl.invokeDelegatingMethodAccessorImpl.java:43
    at java.lang.reflect.Method.invokeMethod.java:498
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflectionAopUtils.java:302
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpointReflectiveMethodInvocation.java:190
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceedReflectiveMethodInvocation.java:157
    at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invokePersistenceExceptionTranslationInterceptor.java:136
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceedReflectiveMethodInvocation.java:179
    at org.springframework.aop.framework.JdkDynamicAopProxy.invokeJdkDynamicAopProxy.java:208
    at com.sun.proxy.$Proxy84.saveUnknown Source
    at com.zihexin.base.service.MongoService.saveRequestLogMongoService.java:44
    at com.zihexin.ZhxpayCommonApplicationTests.test4ZhxpayCommonApplicationTests.java:72
    at sun.reflect.NativeMethodAccessorImpl.invoke0Native Method
    at sun.reflect.NativeMethodAccessorImpl.invokeNativeMethodAccessorImpl.java:62
    at sun.reflect.DelegatingMethodAccessorImpl.invokeDelegatingMethodAccessorImpl.java:43
    at java.lang.reflect.Method.invokeMethod.java:498
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCallFrameworkMethod.java:50
    at org.junit.internal.runners.model.ReflectiveCallable.runReflectiveCallable.java:12
    at org.junit.runners.model.FrameworkMethod.invokeExplosivelyFrameworkMethod.java:47
    at org.junit.internal.runners.statements.InvokeMethod.evaluateInvokeMethod.java:17

service中代码:

@Service
public class MongoService {

    private static Logger logger = LoggerFactory.getLoggerMongoService.class;

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

    public boolean existsString requestId{
        return merchantRequestLogRepo.existsrequestId;
    }
    //异步保存mongo日志
//    @Async
    public void saveRequestLogString ip,String paramMap, String requestId,String paymentWay{

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

        MerchantRequestLog merchantRequestLog = new MerchantRequestLog;
        merchantRequestLog.setRequestDatenew Date;
        merchantRequestLog.setRequestIdrequestId;
        merchantRequestLog.setRequestParamparamMap;
        merchantRequestLog.setRequestIpip;
        merchantRequestLog.setPaymentWaypaymentWay;
//        merchantRequestLog.setHostIpConstants.hostAddress;
        merchantRequestLogRepo.savemerchantRequestLog;
        logger.info"Async Save Merchant Request Log End ,Spend Time :{} ms",System.currentTimeMillis-start;
    }

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

2点供考虑:

1,乐观锁主要是依靠version来处理冲突的,乐观锁的异常主要是:

1)Insert的时候发现待插入的数据,有更为新的version的数据了,因为有其他的同样的Insert的操作;
2)Update的时候发现待修改的数据,有更为新的version的数据了,因为有其他的同样的Update的操作。

这个需要检查一下应用需求,是否可以接受这种异常;如果接受不了,就需要进一步修改。

2,为什么Save不行,而Insert可以?请留意MongoDB中的Save和Insert的区别。

1)Save如果带_id,有可能是Update;
2)Save如果不带_id,和Insert一样。

简而言之,Save可以是一个Update操作。

供参考。

Love MongoDB! Have Fun!

栏目列表

广告位

澳门新葡京官网