Spring事務(wù)失效的幾種原因
數(shù)據(jù)庫引擎不支持事務(wù)
在MySQL數(shù)據(jù)庫中有幾種引擎(InnoDB,MyISAM,Memory等等),僅僅InnoDB支持事務(wù),如果數(shù)據(jù)庫底層都不支持事務(wù)的話,那么再怎么折騰都是白搭.
@transactional加在private方法上
@Transactional只能加在public方法上,如果需要在private方法中加入事務(wù),可以使用Aspect配transactionManager使用.
本類方法調(diào)本類另一個(gè)方法
例如:
@Service public class UserServiceImpl implements UserService { @Transactional public void update(User user) { //check updateUserInfo(user); } @Transactional(propagation = Propagation.REQUIRES_NEW) public void updateUser(User user) { // update user } }
@Transactional(propagation = Propagation.REQUIRES_NEW)是無效的,在Spring中是使用代理的方式實(shí)現(xiàn)事務(wù),發(fā)生自身調(diào)用的時(shí)候,沒有經(jīng)過Spring的代理,自然事務(wù)失效.
不支持事務(wù)
@Service public class UserServiceImpl implements UserService { @Transactional(propagation = Propagation.NOT_SUPPORTED) public void update(User user) { //do some action } }
@Transactional(propagation = Propagation.NOT_SUPPORTED)表示如果當(dāng)前存在事務(wù)就掛起,以沒有事務(wù)的方式運(yùn)行,主動(dòng)不支持事務(wù)了,那么再怎么操作也是白搭. 此處貼下Spring的傳播行為:
/** * Support a current transaction, create a new one if none exists. * Analogous to EJB transaction attribute of the same name. * <p>This is the default setting of a transaction annotation. */ REQUIRED(TransactionDefinition.PROPAGATION_REQUIRED), /** * Support a current transaction, execute non-transactionally if none exists. * Analogous to EJB transaction attribute of the same name. * <p>Note: For transaction managers with transaction synchronization, * PROPAGATION_SUPPORTS is slightly different from no transaction at all, * as it defines a transaction scope that synchronization will apply for. * As a consequence, the same resources (JDBC Connection, Hibernate Session, etc) * will be shared for the entire specified scope. Note that this depends on * the actual synchronization configuration of the transaction manager. * @see org.springframework.transaction.support.AbstractPlatformTransactionManager#setTransactionSynchronization */ SUPPORTS(TransactionDefinition.PROPAGATION_SUPPORTS), /** * Support a current transaction, throw an exception if none exists. * Analogous to EJB transaction attribute of the same name. */ MANDATORY(TransactionDefinition.PROPAGATION_MANDATORY), /** * Create a new transaction, and suspend the current transaction if one exists. * Analogous to the EJB transaction attribute of the same name. * <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box * on all transaction managers. This in particular applies to * {@link org.springframework.transaction.jta.JtaTransactionManager}, * which requires the {@code javax.transaction.TransactionManager} to be * made available to it (which is server-specific in standard Java EE). * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager */ REQUIRES_NEW(TransactionDefinition.PROPAGATION_REQUIRES_NEW), /** * Execute non-transactionally, suspend the current transaction if one exists. * Analogous to EJB transaction attribute of the same name. * <p><b>NOTE:</b> Actual transaction suspension will not work out-of-the-box * on all transaction managers. This in particular applies to * {@link org.springframework.transaction.jta.JtaTransactionManager}, * which requires the {@code javax.transaction.TransactionManager} to be * made available to it (which is server-specific in standard Java EE). * @see org.springframework.transaction.jta.JtaTransactionManager#setTransactionManager */ NOT_SUPPORTED(TransactionDefinition.PROPAGATION_NOT_SUPPORTED), /** * Execute non-transactionally, throw an exception if a transaction exists. * Analogous to EJB transaction attribute of the same name. */ NEVER(TransactionDefinition.PROPAGATION_NEVER), /** * Execute within a nested transaction if a current transaction exists, * behave like PROPAGATION_REQUIRED else. There is no analogous feature in EJB. * <p>Note: Actual creation of a nested transaction will only work on specific * transaction managers. Out of the box, this only applies to the JDBC * DataSourceTransactionManager when working on a JDBC 3.0 driver. * Some JTA providers might support nested transactions as well. * @see org.springframework.jdbc.datasource.DataSourceTransactionManager */ NESTED(TransactionDefinition.PROPAGATION_NESTED);
異常被catch
@Service public class UserServiceImpl implements UserService { @Transactional public void update(User user) { try{ }catch(Exception e){ log.error(e.getMessage(),e); } } }
觸發(fā)回滾的操作是被接收到異常,一般我們會(huì)在@Transactional后面加上rollbackFor或者noRollbackForClassName來指明觸發(fā)回滾的異常,但是如果在代碼中給catch了異常,那么對(duì)于Spring代理來說就這個(gè)方法從頭到尾都沒有問題,自然不會(huì)觸發(fā)回滾.
異常類型錯(cuò)誤
@Service public class UserServiceImpl implements UserService { @Transactional public void update(User user) { try{ }catch(Exception e){ log.error(e.getMessage(),e); throw new Exception(e.getMessage()); } } }
以上方式throw new Exception(e.getMessage());事務(wù)也是無效的,主要原因是事務(wù)回滾的條件是throw 運(yùn)行時(shí)異常(RunTimeException).如果需要其他異常也回滾,需要在@Transactional后面加上rollbackFor或者noRollbackForClassName來指明觸發(fā)回滾的異常.
沒有被Spring管理
不在Spring環(huán)境下,自然不受Spring的管理,事務(wù)管理器也當(dāng)然失去了作用.
沒有配置TransactionManager
需要對(duì)當(dāng)前數(shù)據(jù)源配置事務(wù)管理器,尤其是在多數(shù)據(jù)源的情況下.
以上就是Spring事務(wù)失效的幾種原因的詳細(xì)內(nèi)容,更多關(guān)于Spring事務(wù)失效的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- Spring詳細(xì)講解事務(wù)失效的場(chǎng)景
- Spring事務(wù)失效場(chǎng)景實(shí)例詳解
- Spring事務(wù)失效場(chǎng)景的詳細(xì)整理
- 分析Springboot中嵌套事務(wù)失效原因詳解
- Spring事務(wù)失效的一種原因關(guān)于this調(diào)用的問題
- 一篇文章帶你了解spring事務(wù)失效的多種場(chǎng)景
- 解決try-catch捕獲異常信息后Spring事務(wù)失效的問題
- 從Spring源碼解析事務(wù)失效的原因
- Spring事務(wù)失效場(chǎng)景原理及解決方案
- Spring事務(wù)失效問題分析及解決方案
- Spring事務(wù)失效的各種場(chǎng)景(13種)
相關(guān)文章
Java漢字轉(zhuǎn)拼音工具類完整代碼實(shí)例
這篇文章主要介紹了java漢字轉(zhuǎn)拼音工具類完整代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03用intellij Idea加載eclipse的maven項(xiàng)目全流程(圖文)
這篇文章主要介紹了用intellij Idea加載eclipse的maven項(xiàng)目全流程(圖文),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-12-12java反射_改變private中的變量及方法的簡(jiǎn)單實(shí)例
下面小編就為大家?guī)硪黄猨ava反射_改變private中的變量及方法的簡(jiǎn)單實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06Java中的SynchronousQueue阻塞隊(duì)列及使用場(chǎng)景解析
這篇文章主要介紹了Java中的SynchronousQueue阻塞隊(duì)列及使用場(chǎng)景解析,SynchronousQueue 是 Java 中的一個(gè)特殊的阻塞隊(duì)列,它的主要特點(diǎn)是它的容量為0,這意味著 SynchronousQueue不會(huì)存儲(chǔ)任何元素,需要的朋友可以參考下2023-12-12springtask 的使用方法和 cron 表達(dá)式解析
這篇文章主要介紹了springtask 的使用方法和 cron 表達(dá)式解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10Java實(shí)現(xiàn)計(jì)算圖中兩個(gè)頂點(diǎn)的所有路徑
這篇文章主要為大家詳細(xì)介紹了如何利用Java語言實(shí)現(xiàn)計(jì)算圖中兩個(gè)頂點(diǎn)的所有路徑功能,文中通過示例詳細(xì)講解了實(shí)現(xiàn)的方法,需要的可以參考一下2022-10-10詳解spring-boot actuator(監(jiān)控)配置和使用
本篇文章主要介紹了spring-boot actuator(監(jiān)控)配置和使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09