[英]Spring Transaction doesn't Rollback at RuntimeException in @Transactional method
[英]Spring hides RuntimeException thrown by a @Transactional method
作為我的代碼的一部分,@ @Transactional
注釋方法。 在某些情況下,該方法將引發自定義異常並執行回滾。 每當這種情況發生時,方法調用都會導致TransactionSystemException
並且無法獲取原始的自定義異常(這不是Exception的根本原因)。
我可以以某種方式訪問我的方法拋出的原始異常嗎? 我可以阻止spring拋出TransactionSystemException,但是仍然回滾嗎?
@Transactional
public Association create(Long accountId) {
// if association already exists, throw exception
throw new ErrorInfoException(ErrorInfo.duplicateAssociation(accountId).info("This Association already exists").build());
}
不確定如何在該方法之外管理異常,但是您需要捕獲TransactionSystemException
,然后在您的情況下調用getApplicationException()
因為自定義異常應首先在堆棧上:
返回在此事務異常之前引發的應用程序異常(如果有)。
如果由於某種原因,在它們之間還有更多的異常, getOriginalException()
,該getOriginalException()
根據規范應:
返回失敗事務中第一個引發的異常:即應用程序異常(如果有)或TransactionSystemException自身原因。
如果使用Exception的getCause
方法,則在這種情況下可能不是正確的方法。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.