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下3中XML解析 DOM方式、SAX方式和StAX方式
目前我知道的JAVA解析XML的方式有:DOM, SAX, StAX;如果選用這幾種,感覺還是有點(diǎn)麻煩;如果使用:JAXB(Java Architecture for XML Binding),個(gè)人覺得太方便了2013-04-04
Spring Cache自定義緩存key和過期時(shí)間的實(shí)現(xiàn)代碼
使用 Redis的客戶端 Spring Cache時(shí),會(huì)發(fā)現(xiàn)生成 key中會(huì)多出一個(gè)冒號(hào),而且有一個(gè)空節(jié)點(diǎn)的存在,查看源碼可知,這是因?yàn)?nbsp;Spring Cache默認(rèn)生成key的策略就是通過兩個(gè)冒號(hào)來拼接,本文給大家介紹了Spring Cache自定義緩存key和過期時(shí)間的實(shí)現(xiàn),需要的朋友可以參考下2024-05-05
淺談SpringBoot Bean加載優(yōu)先級(jí)的問題
這篇文章主要介紹了淺談SpringBoot Bean加載優(yōu)先級(jí)的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
mybatis教程之增刪改查_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了mybatis教程之增刪改查,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-09-09
java基于netty NIO的簡(jiǎn)單聊天室的實(shí)現(xiàn)
這篇文章主要介紹了java基于netty NIO的簡(jiǎn)單聊天室的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Java 可視化垃圾回收_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
Ben Evans是一名資深培訓(xùn)師兼顧問,他在演講可視化垃圾回收中從基礎(chǔ)談起討論了垃圾回收。以下是對(duì)其演講的簡(jiǎn)短總結(jié)。感興趣的朋友一起學(xué)習(xí)吧2017-05-05
Netty實(shí)現(xiàn)自定義協(xié)議編解碼器
這篇文章主要為大家介紹了Netty實(shí)現(xiàn)自定義協(xié)議編解碼器示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

