之前写了一些辅助工作相关的Spring Boot怎么使用AOP。这里继续正题,怎么减少Spring Boot 乐观锁加锁报错的情况(基本可以解决)。

1. 包依赖

  • spring-boot-starter-data-jpa, Spring Boot的JPA starter
  • h2, H2内存数据库
  • spring-boot-starter-test,Spring Boot的Junit测试starter
         <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>1.2.6.RELEASE</version>
</dependency> <dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.188</version>
<scope>runtime</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<version>1.2.6.RELEASE</version>
<scope>test</scope>
</dependency>

2. 如何在启用乐观锁?

我用的是JPA, 所以很简单,在实体类加一个字段,并注解@Version。

 @Entity
public class Account { //primary key, auto generated
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id; private String name; // enable optimistic locking version control
@Version
private int version; /*omitted getter/setter, but required*/
}

3. 通过AOP实现对RetryOnOptimisticLockingFailureException的恢复

为了减少对代码的侵入,对之前的AOP例子进行少许修改:

  • 自定义一个注解,用来标注需要恢复这个错误的接口
 @Retention(RetentionPolicy.RUNTIME)
public @interface RetryOnOptimisticLockingFailure { }
  • 切入点表达式使用注解,不再使用execution
     @Pointcut("@annotation(RetryOnOptimisticLockingFailure)")
public void retryOnOptFailure() {
// pointcut mark
}
  
@Around("retryOnOptFailure()")
public Object doConcurrentOperation(ProceedingJoinPoint pjp) throws Throwable {
int numAttempts = 0;
do {
numAttempts++;
try {
return pjp.proceed();
} catch (OptimisticLockingFailureException ex) {
if (numAttempts > maxRetries) {
//log failure information, and throw exception
throw ex;
}else{
//log failure information for audit/reference
//will try recovery
}
}
} while (numAttempts <= this.maxRetries); return null;
}
  • 在需要对错误进行恢复的RESTFul接口加上恢复标签

至于为什么一定是要在RESTFul接口上加,而不是其他地方(例如service层),是因为Spring Boot的事务管理的上下文是从resource层开始建立的,在service层恢复是无效的,因为数据库的操作依然是在之前失败的事务里,之后再细说吧。

 @RestController
@RequestMapping("/account")
public class AccountResource { @Autowired
private AccountService accountService; @RequestMapping(value = "/{id}/{name}", method = RequestMethod.PUT)
@ResponseBody
@RetryOnOptimisticLockingFailure
public void updateName(@PathVariable Integer id, @PathVariable String name) {
accountService.updateName(id, name);
}
}

4. 测试用例

@Test
public void testUpdate() {
new Thread(() -> this.client.put(base + "/1/llt-2", null)).start();
new Thread(() -> this.client.put(base + "/1/llt-3", null)).start(); try {
//waiting for execution result of service
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

5. 测试一下效果如何

  • 没有在AccountResource的updateName方法加@RetryOnOptimisticLockingFailure:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.ObjectOptimisticLockingFailureException: Object of class [com.leolztang.sb.aop.model.Account] with identifier [1]: optimistic locking failed; nested exception is org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.leolztang.sb.aop.model.Account#1]] with root cause

org.hibernate.StaleObjectStateException: Row was updated or deleted by another transaction (or unsaved-value mapping was incorrect) : [com.leolztang.sb.aop.model.Account#1]
at org.hibernate.persister.entity.AbstractEntityPersister.check(AbstractEntityPersister.java:2541)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:3285)
  • 在AccountResource的updateName方法加@RetryOnOptimisticLockingFailure:
Original:name=[llz-1],version=[0],New:name=[llt-2],version=[1]
Original:name=[llt-2],version=[1],New:name=[llt-3],version=[2]

6. 完整代码

http://files.cnblogs.com/files/leolztang/sb.aop-v2.tar.gz

最新文章

  1. 关于清除arp 缓存的那点事儿
  2. 进击的Python【第十章】:Python的socket高级应用(多进程,协程与异步)
  3. Redis服务器配置
  4. Linux 网络工具之fping
  5. 谈谈 css 的各种居中——读编写高质量代码有感
  6. MySQL与Oracle的语法区别详细对比 (转)
  7. 更好列表页中一个航班.先unset删除数组中一个键值对,再追加,最后按键排序
  8. Intersect交集
  9. min-max容斥 hdu 4336 &amp;&amp; [BZOJ4036] 按位或
  10. 常用的三种json软件的使用
  11. SQL语法基础之CREATE语句
  12. IDEA窗口重置
  13. 【转】XML 特殊字符处理
  14. hdu4670 树分治
  15. synchronized(六)
  16. k-SLAM:k-mer Sorted List Alignment and Metagenomics
  17. Struts 2 学习笔记
  18. BZOJ.2007.[NOI2010]海拔(最小割 对偶图最短路)
  19. itunes connect 改版后无法访问的处理办法
  20. Netty-gRPC介绍和使用

热门文章

  1. 极路由2(极贰)在OpenWrt下定制自己的ss服务
  2. [LeetCode] Largest Divisible Subset 最大可整除的子集合
  3. [LeetCode] Strobogrammatic Number III 对称数之三
  4. 在 sublime 中运行 JavaScript 代码
  5. TCP进制转换
  6. 如何利用git由本机向github上传文件
  7. python基础补漏-04-常用函数
  8. 聊聊 Apache 开源协议
  9. 图说C++对象模型:对象内存布局详解
  10. UltralEdit 替换tips