mongodb启动不能锁定

在我以前的文章中,我谈到了对MongoDB批处理程序采用乐观锁定的好处。 如我之前所写,乐观锁定异常是可恢复的异常,只要我们获取最新的Entity,我们就会对其进行更新并保存。

因为我们使用的是MongoDB,所以我们不必担心本地或XA事务。 在以后的文章中,我将演示如何在使用JPA时构建相同的机制。

Spring框架提供了很好的AOP支持,因此可以轻松实现自动重试机制,这就是我做到的方式。

我们首先定义一个Retry注释:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Retry {Class<? extends Exception>[] on();int times() default 1;
}

我们注释了我们的业务逻辑方法,例如

@Retry(times = 10, on = org.springframework.dao.OptimisticLockingFailureException.class)
public Product updateName(Long id, String name) {Product product = productRepository.findOne(id);product.setName(name);LOGGER.info("Updating product {} name to {}", product, name);return productRepository.save(product);
}

然后,我们只需要AOP方面来拦截业务逻辑调用,并在乐观锁定检测的情况下重试。

@Aspect
public class OptimisticConcurrencyControlAspect {private static final Logger LOGGER = LoggerFactory.getLogger(OptimisticConcurrencyControlAspect.class);@Around("@annotation(vladmihalcea.concurrent.Retry)")public Object retry(ProceedingJoinPoint pjp) throws Throwable {Retry retryAnnotation = getRetryAnnotation(pjp);return (retryAnnotation != null) ? proceed(pjp, retryAnnotation) : proceed(pjp);}private Object proceed(ProceedingJoinPoint pjp) throws Throwable {return pjp.proceed();}private Object proceed(ProceedingJoinPoint pjp, Retry retryAnnotation) throws Throwable {int times = retryAnnotation.times();Class<? extends Throwable>[] retryOn = retryAnnotation.on();Assert.isTrue(times > 0, "@Retry{times} should be greater than 0!");Assert.isTrue(retryOn.length > 0, "@Retry{on} should have at least one Throwable!");LOGGER.info("Proceed with {} retries on {}", times, Arrays.toString(retryOn));return tryProceeding(pjp, times, retryOn);}private Object tryProceeding(ProceedingJoinPoint pjp, int times, Class<? extends Throwable>[] retryOn) throws Throwable {try {return proceed(pjp);} catch (Throwable throwable) {if(isRetryThrowable(throwable, retryOn) && times-- > 0) {LOGGER.info("Optimistic locking detected, {} remaining retries on {}", times, Arrays.toString(retryOn));return tryProceeding(pjp, times, retryOn);}throw throwable;}}private boolean isRetryThrowable(Throwable throwable, Class<? extends Throwable>[] retryOn) {Throwable[] causes = ExceptionUtils.getThrowables(throwable);for(Throwable cause : causes) {for(Class<? extends Throwable> retryThrowable : retryOn) {if(retryThrowable.isAssignableFrom(cause.getClass())) {return true;}}}return false;}private Retry getRetryAnnotation(ProceedingJoinPoint pjp) throws NoSuchMethodException {MethodSignature signature = (MethodSignature) pjp.getSignature();Method method = signature.getMethod();Retry retryAnnotation = AnnotationUtils.findAnnotation(method, Retry.class);if(retryAnnotation != null) {return retryAnnotation;}Class[] argClasses = new Class[pjp.getArgs().length];for (int i = 0; i < pjp.getArgs().length; i++) {argClasses[i] = pjp.getArgs()[i].getClass();}method = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), argClasses);return AnnotationUtils.findAnnotation(method, Retry.class);}
}

该测试开始10个竞标以竞争产品的保存,这就是测试日志。

Line 492: INFO  [Thread-9]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 495: INFO  [Thread-3]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 504: INFO  [Thread-8]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 505: INFO  [Thread-11]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 507: INFO  [Thread-10]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 513: INFO  [Thread-5]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 523: INFO  [Thread-4]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 9 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 529: INFO  [Thread-3]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 8 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 586: INFO  [Thread-10]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 8 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 682: INFO  [Thread-5]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 8 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 683: INFO  [Thread-3]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 7 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 686: INFO  [Thread-8]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 8 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 702: INFO  [Thread-3]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 6 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 752: INFO  [Thread-5]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 7 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 756: INFO  [Thread-8]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 7 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]Line 859: INFO  [Thread-5]: v.c.a.OptimisticConcurrencyControlAspect - Optimistic locking detected, 6 remaining retries on [class org.springframework.dao.OptimisticLockingFailureException]
  • 代码可在GitHub上获得 。
参考: Vlad Mihalcea博客博客上的JCG合作伙伴 Vlad Mihalcea 对MongoDB进行了乐观锁定重试 。

翻译自: https://www.javacodegeeks.com/2013/11/optimistic-locking-retry-with-mongodb.html

mongodb启动不能锁定

mongodb启动不能锁定_使用MongoDB进行乐观锁定重试相关推荐

  1. 修改mongodb最大查询数_关于MongoDB最大连接数的查看与修改

    在Linux平台下,无论是64位或者32位的MongoDB默认最大连接数都是819,WIN平台不知道,估计也没有人在 WIN平台下使用MongoDB做生产 在Linux平台下,无论是64位或者32位的 ...

  2. mongodb mysql json数据_使用MongoDB与MySQL有很多JSON字段?

    所以,直接回答问题- Shall we chose mongodb if half of data is schemaless, and is being stored as JSON if usin ...

  3. mongodb 服务器时区设置_关于MongoDB时区问题

    由于MongoDb存储时间按照UTC时间存储的,其官方驱动MongoDB.driver存储时间的时候将本地时间转换为了utc时间,但它有个蛋疼的bug,读取的时候非常蛋疼的是返回的是utc使时间.一个 ...

  4. mongodb更新某个字段_直播 | MongoDB开源数据库的云上之路

    本文转自阿里巴巴数据库技术 来了来了!就在今天(11月26号)阿里云-MongoDB战略合作发布会真的来了!一直被模仿从未没超越的开源界大牛MongoDB与亚太区云数据库TOP 1 阿里云数据库要联手 ...

  5. mongodb 导出 带条件_将 MongoDB 导出成 csv

    **[摘要]** 将 Mongodb 数据结构转换成结构化的数据需求,我们可利用集算器 SPL 语言来进行辅助实现.若想了解更多,请前往乾学院:将 MongoDB 导出成 csv! 来源:https: ...

  6. mongodb创建图书管理_基于MongoDB的云数据库管理系统的设计与实现

    III 目 录 摘 要 ........................................................................................ ...

  7. mongodb mysql 知乎_为什么 MongoDB 索引选择B-树,而 Mysql 索引选择B+树(精干总结)...

    本文献给准备面试或者是还在面试的你.常见面试题,送分题目,不拿白不拿.本文收录在个人博客<愚公要移山>中,地址 www.javachat.cc 这篇是修改版,针对知乎上很多人提出的问题,进 ...

  8. 修改mongodb最大查询数_根据Mongodb查询中的一个字段查找最大连续记录数

    我想根据一个特定字段找到最大连续记录的数量. 在根据字段查找排序后,我的db.people集合是: > db.people.find().sort({ updated_at: 1}) { &qu ...

  9. 使用弹簧启动和 JPA 测试乐观锁定处理

    虽然 JPA 中的乐观锁定处理是相对众所周知的,但它通常测试得很差或根本没有测试. 在这篇博文中,我将首先向您展示乐观锁定处理的含义,以及如何在 Spring 引导应用程序和 JPA 中实现它. 之后 ...

最新文章

  1. Javascript 面向对象全新理练之数据的封装
  2. python memory usage_Python Pandas Index.memory_usage()用法及代码示例
  3. 【C++】Visual Studio教程(六) -更新Visual Studio
  4. Android 图片切换特效 AndroidImageSlider
  5. Struts框架原理分析之我见
  6. 12星座程序员写代码
  7. mysql禁止远程连接
  8. 一、GeoLabel:全网最好用的遥感样本标注软件
  9. IT负载率与数据中心规模——孙长青
  10. 海尔简爱S11笔记本BIOS设置U盘启动教程
  11. Python--站在巨人肩膀上
  12. Codecademy-中文JavaScript系列教程-初认JS
  13. 进化算法的产生与发展
  14. arm-2d库详细介绍
  15. 删除文件夹时,显示“错误0x80070091 文件夹不是空的”
  16. 电影主题HTM5网页设计作业成品——爱影评在线电影(10页面)使用dreamweaver制作采用DIV+CSS进行布局
  17. 元宇宙大火的“天时、地利、人和”
  18. 文档集数据处理 gensim corpora.Dictionary
  19. 自律训练法 John Sehorz
  20. 提升设计性能的HDL编码方法

热门文章

  1. HDU5322 - cdq分治FFT加速dp
  2. 15、java中的集合(2)
  3. Java NIO系列教程(四) Scatter/Gather
  4. 分布式一致性算法:可能比你想象得更复杂
  5. React中判断已完成的个数
  6. springboot获取多个请求参数_springboot获取URL请求参数的多种方式
  7. 2-6 基于SpringBoot的SpringSecurity环境快速搭建与验证
  8. 第1步 新建spring+springmvc+mybatis项目 每步都有 讲解 巨详细
  9. mysql数据横表变成竖表_MySQL中横表和竖表相互转换
  10. ssh无密码登陆权威指南