溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

23. Spring 事務(wù)注解@Transactional和異常捕獲

發(fā)布時間:2020-07-01 18:50:26 來源:網(wǎng)絡(luò) 閱讀:1413 作者:rongwei84n 欄目:開發(fā)技術(shù)

一. 事務(wù)注解限制條件

1. 不允許在private方法上面


2. 不能在非事務(wù)方法里面調(diào)用事務(wù)方法


二. 實現(xiàn)機制-異常捕獲

Describes transaction attributes on a method or class.
This annotation type is generally directly comparable to Spring's org.springframework.transaction.interceptor.RuleBasedTransactionAttribute class, and in fact AnnotationTransactionAttributeSource will directly convert the data to the latter class, so that Spring's transaction 
support code does not have to know about annotations. If no rules are relevant 
to the exception, it will be treated like org.springframework.transaction.interceptor.DefaultTransactionAttribute (rolling back on RuntimeException and Error but not on checked exceptions).
For specific information about the semantics of this annotation's attributes, 
consult the org.springframework.transaction.TransactionDefinition and org.springframework.transaction.interceptor.TransactionAttribute javadocs.

所以如果操作數(shù)據(jù)庫的時候把異常捕獲了,那么將不能回滾。


三. 手動回滾

TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

所以為了解決這種矛盾,可以捕獲異常后,在catch里面手動調(diào)用回滾。


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI