[英]How to rollback transaction on calling @Transactional and non-Transactional method in same and different Service?
I am using spring data rest and Spring JPA. 我正在使用Spring Data Rest和Spring JPA。 I am having one method which update one database table.
我有一种更新一个数据库表的方法。
@Autowired InvoiceClient;
@Override
@Transactional
public String doBilling(String x){
//get date from TableOne
Bill bill = billsRepository.getBill(x);
if(bill.isPaid()){
generateInvoice();
}
bill.setPaymentDate(new Date());
return "SUCCESS";
}
generateInvoice is non Transactional method which calls @Transactional method from other service. generateInvoice是非事务性方法,它从其他服务调用@Transactional方法。
public void generateInvoice(){
invoiceClient.generateInvoice();//this is @Transactional, make changes in TableTwo
}
In case of any exception in generateInvoice method whole transaction is rolled back. 如果generateInvoice方法中有任何异常,则会回滚整个事务。 Now I want to add one more method which will have list of bill numbers.
现在,我想添加另一种方法,该方法将具有清单编号列表。 I call doBilling method in loop to do billing for all the bills.
我循环调用doBilling方法为所有账单开票。
@Override
@Transactional(readOnly = false, rollbackFor = {Throwable.class}, propagation = Propagation.REQUIRED)
public String doBillingForAll(List<String> tx){
for(String x: tx){
doBilling(x);
}
}
But now in case of any exceptions in doBilling method, all the setPayment methods are getting rolled back but generateInvoice is persisted. 但是现在,在doBilling方法中发生任何异常的情况下,所有setPayment方法都将回滚,但是generateInvoice将保留下来。 I want to rollback generateInvoice also.
我也想回滚generateInvoice。 How can I do it?
我该怎么做?
You don't need to define a rollbackFor = {Throwable.class}
. 您无需定义
rollbackFor = {Throwable.class}
。 By default all RuntimeException
do a rollback when using @Transactional
. 默认情况下,使用
@Transactional
时,所有RuntimeException
都会回滚。
It can be that because you are using and intermediate non @Transactional
annotated method, the main Transaction is suspended and a nested one is created. 可能是因为您使用的是非
@Transactional
注释的中间方法,所以主事务被挂起并创建了一个嵌套的事务。
Try to put @Transactional
in your public void generateInvoice()
then Propagation.REQUIRED
should be applied with rollback of your invoices 尝试将
@Transactional
放入您的public void generateInvoice()
然后将Propagation.REQUIRED
与您的发票回滚一起应用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.