如何解决@Transactional 和 @Retryable 的问题
如果我从 spring-retry 库中添加 @Retryable
,我将无法在事务中执行数据库操作。这是我的代码结构的样子:
public class ExpireAndSaveTrades {
@Transactional(rollbackFor = MyException.class)
public void expireAndSaveTrades(List<Trade> trades) {
try {
// these two MUST be executed in one transaction
trades.forEach(trade -> dao.expireTrades(trade));
dao.saveTrades(trades);
} catch (Exception e) {
throw new MyException(e.getMessage(),e);
}
}
}
public class Dao {
@Retryable(value = CannotAcquireLockException.class,maxAttempts = 3,stateful = true,backoff = @Backoff(delay = 300,multiplier = 3))
public void expireTrades(Trade trade) {
try {
tradeRepository.expire(trade.getId(),trade.getNewStopDate());
} catch (CannotAcquireLockException e) {
expireTrade(trade);
}
}
@Retryable(value = CannotAcquireLockException.class,multiplier = 3))
public void saveTrades(List<Trades> trades) {
try {
tradeRepository.saveAll(trades)
} catch (CannotAcquireLockException e) {
saveTrades(trades);
}
}
}
public interface TradeRepository extends JpaRepository<Trade,Integer> {
@Modifying
@Query(value = "update trade set stop_date=:new_stop_date where id=:id",nativeQuery = true)
void expire(@Param("id") int id,@Param("new_stop_date") String newStopDate);
}
所以这就是我现在所在的位置:
- 不使用有状态(即有状态默认设置为
false
) - 重试成功,但在结束时,我看到此异常:org.springframework.transaction.UnexpectedRollbackException: Transaction silently rolled back because it has been marked as rollback-only
和更新/保存的数据多次重试后回滚数据库表 - stateful = true - 不再发生重试
我浏览了许多 SO 帖子和博客,但找不到解决我的问题的方法。这里有人可以帮我吗?
编辑: 更新了我的问题以添加 try-catch 块这样,spring-retry 不会启动(我知道是因为我向 @Retryable
添加了一个侦听器来记录 { {1}}。我没有看到日志被打印出来。如果出现CannotAcquireLockException,事务也会静默回滚
retryContext
解决方法
您正在事务中进行重试;这是错误的,会产生你看到的结果;您需要交换它并在重试中执行事务。这就是为什么在不使用 stateful 时会出现回滚错误的原因。
如果您使用有状态重试,则 @Retryable
所做的只是保留状态;可重试的调用者必须继续调用直到成功或重试耗尽。
编辑
这是一个使用有状态重试的例子
@Component
class ServiceCaller {
@Autowired
Service service;
public void call() {
try {
this.service.process();
}
catch (IllegalStateException e) {
System.out.println("retrying...");
call();
}
catch (RuntimeException e) {
throw e;
}
}
}
@Component
class Service {
@Autowired
Retryer retryable;
@Transactional
public void process() {
retryable.invoke();
}
}
@Component
class Retryer {
@Retryable(maxAttempts = 3,stateful = true)
public void invoke() {
System.out.println("Invoked");
throw new IllegalStateException("failed");
}
@Recover
public void recover(IllegalStateException e) {
System.out.println("Retries exhausted");
throw new RuntimeException(e);
}
}
Invoked
retrying...
Invoked
retrying...
Invoked
retrying...
Retries exhausted
...
Caused by: java.lang.RuntimeException: java.lang.IllegalStateException: failed
at com.example.demo.Retryer.recover(So67197577Application.java:84) ~[classes/:na]
...
Caused by: java.lang.IllegalStateException: failed
而且,没有 @Recover
方法...
Invoked
retrying...
Invoked
retrying...
Invoked
retrying...
...
Caused by: org.springframework.retry.ExhaustedRetryException: Retry exhausted after last attempt with no recovery path; nested exception is java.lang.IllegalStateException: failed
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。