小编kna*_*983的帖子

Spring乐观锁定:如何重试事务方法直到提交成功

我使用Spring 2.5和Hibernate JPA实现Java和"容器"管理事务.

我有一个"用户提交后"方法,它在后台更新数据,无论是异常ConcurrencyFailureException还是StaleObjectStateException异常都需要提交,因为它永远不会显示给客户端.换句话说,需要将乐观锁定变为悲观.(如果方法执行需要更长的时间并且有人在其他事务中更改了数据,则可能发生)


我读了很多关于幂等的东西,如果异常搜索DEFAULT_MAX_RETRIES6.2.7则重试.示例第14.5章.重试.我也在这里这里找到了stackoverflow .

我试过这个:

public aspect RetryOnConcurrencyExceptionAspect {

    private static final int DEFAULT_MAX_RETRIES = 20;
    private int maxRetries = DEFAULT_MAX_RETRIES;

    Object around(): execution( * * (..) ) && @annotation(RetryOnConcurrencyException) && @annotation(Transactional) {

        int numAttempts = 0;
          RuntimeException failureException = null;
          do {
                numAttempts++;
                try {
                    return proceed(); 
                } 
                catch( OptimisticLockingFailureException ex ) {
                    failureException = ex;
                }
                catch(ConcurrencyFailureException ex) { …
Run Code Online (Sandbox Code Playgroud)

java spring transactions aspectj optimistic-locking

19
推荐指数
2
解决办法
1万
查看次数

标签 统计

aspectj ×1

java ×1

optimistic-locking ×1

spring ×1

transactions ×1