色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

java - 保存日志到mongo失敗,樂(lè)觀鎖異常

瀏覽:104日期:2024-01-19 14:57:29

問(wèn)題描述

想把mongo保存進(jìn)mongoDB中,使用的spring-boot-data-mongo.在dao層可以正常保存,但是寫(xiě)在service中報(bào)錯(cuò):

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中代碼:

@Servicepublic class MongoService { private static Logger logger = LoggerFactory.getLogger(MongoService.class); /**商戶(hù)請(qǐng)求相關(guān)*/ //判斷流水號(hào)重復(fù)或者為空 @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()后就可以正常保存了,想不明白為什么,希望大家?guī)兔獯鹨幌?謝謝!

問(wèn)題解答

回答1:

2點(diǎn)供考慮:

1,樂(lè)觀鎖主要是依靠version來(lái)處理沖突的,樂(lè)觀鎖的異常主要是:

1)Insert的時(shí)候發(fā)現(xiàn)待插入的數(shù)據(jù),有更為新的version的數(shù)據(jù)了,因?yàn)橛衅渌耐瑯拥腎nsert的操作;2)Update的時(shí)候發(fā)現(xiàn)待修改的數(shù)據(jù),有更為新的version的數(shù)據(jù)了,因?yàn)橛衅渌耐瑯拥腢pdate的操作。這個(gè)需要檢查一下應(yīng)用需求,是否可以接受這種異常;如果接受不了,就需要進(jìn)一步修改。

2,為什么Save不行,而Insert可以?請(qǐng)留意MongoDB中的Save和Insert的區(qū)別。

1)Save如果帶_id,有可能是Update;2)Save如果不帶_id,和Insert一樣。簡(jiǎn)而言之,Save可以是一個(gè)Update操作。

供參考。

Love MongoDB! Have Fun!

標(biāo)簽: java
主站蜘蛛池模板: 久久精品免费观看视频 | 91久久网 | 国产免费久久精品99 | xxxwww黄色 | 成年女人在线观看片免费视频 | 男操女免费视频 | aaa免费毛片 | 亚洲男女免费视频 | 亚洲精品久久久久久久福利 | 国产特一级毛片 | 久久国产网站 | 国产成人福利免费观看 | 国产视频一区二区三区四区 | 国产精品不卡在线 | 国产偷怕自拍 | japanese色系tube护士 | 热99re久久精品2久久久 | 国产欧美日韩一区二区三区在线 | 超清首页 国产 亚洲 丝袜 | aaa毛片免费观看 | 美女一级免费毛片 | 婷婷在线成人免费观看搜索 | 日韩在线一区二区三区视频 | 亚洲不卡视频在线观看 | 成年人网站在线 | 免费一区二区三区久久 | 高清国产精品久久久久 | 九九久久精品 | 一级女性全黄久久生活片 | 国内精品久久久久久久久久影视 | 亚洲欧美国产一区二区三区 | 毛片3| 成 人色 网 站 欧美大片在线观看 | 国产欧美日韩综合一区二区三区 | 久久99国产精品久久99无号码 | 久久精品福利视频在线观看 | japanesevideo国产在线 | 欧美性猛片xxxxⅹ免费 | 中文字幕乱码中文乱码51精品 | 美女扒开腿被男人猛视频 | 亚洲国产精品免费观看 |