国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Spring事務(wù)失效的幾種原因

瀏覽:38日期:2023-08-14 10:20:58

數(shù)據(jù)庫引擎不支持事務(wù)

在MySQL數(shù)據(jù)庫中有幾種引擎(InnoDB,MyISAM,Memory等等),僅僅InnoDB支持事務(wù),如果數(shù)據(jù)庫底層都不支持事務(wù)的話,那么再怎么折騰都是白搭.

@transactional加在private方法上

@Transactional只能加在public方法上,如果需要在private方法中加入事務(wù),可以使用Aspect配transactionManager使用.

本類方法調(diào)本類另一個方法

例如:

@Servicepublic 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中是使用代理的方式實現(xiàn)事務(wù),發(fā)生自身調(diào)用的時候,沒有經(jīng)過Spring的代理,自然事務(wù)失效.

不支持事務(wù)

@Servicepublic 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ù)的方式運行,主動不支持事務(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

@Servicepublic class UserServiceImpl implements UserService { @Transactional public void update(User user) { try{ }catch(Exception e){ log.error(e.getMessage(),e); } }}

觸發(fā)回滾的操作是被接收到異常,一般我們會在@Transactional后面加上rollbackFor或者noRollbackForClassName來指明觸發(fā)回滾的異常,但是如果在代碼中給catch了異常,那么對于Spring代理來說就這個方法從頭到尾都沒有問題,自然不會觸發(fā)回滾.

異常類型錯誤

@Servicepublic 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 運行時異常(RunTimeException).如果需要其他異常也回滾,需要在@Transactional后面加上rollbackFor或者noRollbackForClassName來指明觸發(fā)回滾的異常.

沒有被Spring管理

不在Spring環(huán)境下,自然不受Spring的管理,事務(wù)管理器也當(dāng)然失去了作用.

沒有配置TransactionManager

需要對當(dāng)前數(shù)據(jù)源配置事務(wù)管理器,尤其是在多數(shù)據(jù)源的情況下.

以上就是Spring事務(wù)失效的幾種原因的詳細(xì)內(nèi)容,更多關(guān)于Spring事務(wù)失效的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 精品日韩欧美一区二区三区 | 日日操夜夜爽 | 国产精品无码久久久久 | 日韩精品在线播放 | 亚洲视频在线免费播放 | 久草福利社 | 草草影院ccyy免费看片 | 精品视频免费在线 | 91b站| 日韩精品一区二区三区乱码 | 精品久久香蕉国产线看观看亚洲 | 高清性色生活片久久久 | 性久久久久久久 | 全部精品孕妇色视频在线 | 日韩精品一区二区三区视频网 | 国产免费人成在线看视频 | 一级爱做片免费观看久久 | 老湿菠萝蜜在线看 | 久久国产乱子伦精品免费不卡 | 人成在线免费视频 | 国产精品国色综合久久 | 能直接看的一级欧美毛片 | 欧美人拘一级毛片 | 久草日韩 | 国内黄色一级精品 | 亚洲情a成黄在线观看动 | 亚洲第一区在线 | 日韩一中文字幕 | 久久一区二区三区免费播放 | se94se欧美 | 国产精品久久久久久福利漫画 | 国产成人精品日本亚洲网站 | 亚洲精品久久久久中文字幕一区 | 免费高清特级毛片 | 欧美一级看片a免费观看 | 免费播放欧美毛片欧美aaaaa | 国产乱码精品一区二区三区中 | 午夜视频一区二区三区 | 国产成人夜间影院在线观看 | 国产成人免费观看在线视频 | 国产一区二三区 |