spring retryable not work for propagation=必需

prdp8dxp  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(308)

@在我的情况下,可复读作品只为

@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中发生异常时回滚计数器。我怎样才能做到这一点?

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题