日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用MongoDB进行乐观锁定重试

發布時間:2023/12/3 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用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进行乐观锁定重试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。