public void completeOrder(OrderRequest request) {
generateOrder(request); // DB call
generateInvoice(request); // DB call
paymentApi(request); // external API call
savePaymentInfo(request); // DB call
createSubscription(request) // DB call
sendInvoiceEmail() // internal API call
}
在上面的代码片段中:
我浏览了一个博客,该博客试图在 TransactionTemplate (https://medium.com/javarevisited/transaction-management-in-spring-boot-eb01e20b21fe) 的帮助下解决问题,但对我不起作用。
如果我看到以下代码的日志,我会得到 HikariPool-1 - Pool stats (total=10, active=1, idle=9, waiting=0) 即使执行了 saveTestDto(testDto)。有什么我需要做额外的事情吗?或者我们有其他的技术来解决这些问题。
@Service("testService")
public class TestServiceImpl implements TestService{
@Autowired
TestRepository testRepository;
@Autowired
private TransactionTemplate template;
@Override
public void testMethod(TestDto testDto) throws InterruptedException {
template.execute(
status -> {
saveTestDto(testDto);
return testDto;
}
);
this.sleep();
template.execute(
status -> {
updateTestDto(testDto);
log.info("Object updated.");
return testDto;
}
);
}
private void saveTestDto(TestDto testDto){
testRepository.saveAndFlush(testDto);
log.info("Object saved.");
}
private void updateTestDto(TestDto testDto){
testDto.setInfo("This is test 1");
testRepository.saveAndFlush(testDto);
log.info("Object saved.");
}
private void sleep() throws InterruptedException {
Thread.sleep(60000);
}
}
我建议确保您没有将对事务管理器的调用打包到另一个事务中。如果您使用的是 spring boot,则每个 rest 请求都可能由于 OSIV
而被包装到事务中在这里您可以在View中了解更多关于Spring Open Session的信息