Spring Retryable 不适用于传播=必需

问题描述

@Retryable 仅适用于我的情况

@Transactional(propagation = Propagation.REQUIRES_NEW)
@Retryable(value = StaleStateException.class)

但不是为了

@Transactional
@Retryable(value = StaleStateException.class)

这是我的代码

   @Log4j2
@Service
@requiredArgsConstructor
public class ServiceA {

  private final CounterService counterService ;

  @Transactional
  public void doJob(String counterType) {
    counterService.getNextValueCounter(counterType);
    
    // do some more jobs
    // can RuntimeException occur
    }
}

@Log4j2
@Service
@requiredArgsConstructor
public class CounterService {

  private final CounterRepository counterRepository;

  @Transactional
  @Retryable(value = StaleStateException.class)
  public Long getNextCounterValue(String counterType) {
    Optional<Counter> counterOptional = counterRepository.findById(counterType);
    if (counterOptional.isPresent()) {
      Counter counter = counterOptional.get();
      if (counter.get().getMaxVersion() != null) {
        counter.setMaxVersion(counter.getMaxVersion() + 1);
        return counter.getMaxVersion();
      } else {
        counter.setMaxVersion(1L);
        return 1L;
      }
    }
    throw new IllegalArgumentException();
  }
}

public interface CounterRepository extends JpaRepository<Counter,String> {

  @Lock(LockModeType.OPTIMISTIC)
  Optional<Counter> findById(String id);
}

我想使用认传播,因为我想在 ServiceA 中发生异常时回滚计数器。我怎样才能做到这一点?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)