如何解决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 举报,一经查实,本站将立刻删除。