[英]Spring rollback transaction part
我配置了spring和hibernate事務(每個服務調用aop並回滾Throwable)。 我需要支持cretePartA()的以下要求:
如果失敗,應該回滾它自己的事務
service1.cteateSmth();
/*started in new transaction*/ public void cteateSmth() { //...some other inserts // should not fail main transaction, // but shoud rollback it's own if is failed anotherService.cretePartA(); //should save state in db in any case recordSmthCreation(); } public void cretePartA(){ updateSubpartA(); updateSubPartA1(); if(!sucees) throw new ValidationException(); }
在上面的示例中,即使嘗試對cretePartA()進行捕獲包裝,整個cteateSmth()事務也將回滾。
我嘗試使用REQUIRES_NEW進行此操作,但在這種情況下似乎無法回滾cretePartA()操作。
TransactionDefinition transactionDefinition = new DefaultTransactionAttribute(TransactionDefinition.Propagation.REQUIRES_NEW);
new TransactionTemplate(transactionManager, transactionDefinition).execute(new TransactionCallback()
{
@Override
public Object doInTransaction(TransactionStatus status)
{
try{
cretePartA()
} catch (Exception e){
}
return null;
}
});
我試圖不為cretePartA()創建新的事務,只是超越了Spring aop的服務調用。 但是在這種情況下,cretePartA()的結果將被認可。
我用cretePartA()方法的注釋@Transactional(propagation = Propagation.REQUIRES_NEW)
解決了這個問題,並嘗試/捕獲了塊
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.