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

您的位置:首頁技術文章
文章詳情頁

Spring事務失效的幾種原因

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

數據庫引擎不支持事務

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

@transactional加在private方法上

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

本類方法調本類另一個方法

例如:

@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中是使用代理的方式實現事務,發生自身調用的時候,沒有經過Spring的代理,自然事務失效.

不支持事務

@Servicepublic class UserServiceImpl implements UserService { @Transactional(propagation = Propagation.NOT_SUPPORTED) public void update(User user) { //do some action }}

@Transactional(propagation = Propagation.NOT_SUPPORTED)表示如果當前存在事務就掛起,以沒有事務的方式運行,主動不支持事務了,那么再怎么操作也是白搭. 此處貼下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); } }}

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

異常類型錯誤

@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());事務也是無效的,主要原因是事務回滾的條件是throw 運行時異常(RunTimeException).如果需要其他異常也回滾,需要在@Transactional后面加上rollbackFor或者noRollbackForClassName來指明觸發回滾的異常.

沒有被Spring管理

不在Spring環境下,自然不受Spring的管理,事務管理器也當然失去了作用.

沒有配置TransactionManager

需要對當前數據源配置事務管理器,尤其是在多數據源的情況下.

以上就是Spring事務失效的幾種原因的詳細內容,更多關于Spring事務失效的資料請關注好吧啦網其它相關文章!

標簽: Spring
相關文章:
主站蜘蛛池模板: 欧美成人午夜片一一在线观看 | 一级女性全黄久久生活片免费 | 成年美女黄网站色视频大全免费 | 美女张开双腿让男人桶 | 欧美中文一区 | 成人毛片免费在线观看 | 亚洲一区二区三区影院 | 另类在线视频 | 久久一级毛片 | 国产成人影院在线观看 | 色欧美hdvideosxs4k | 精品国产综合区久久久久99 | 久久网免费 | 九九免费精品视频在这里 | 久久怡红院亚欧成人影院 | 91精品国产爱久久久久 | 在线观看中文字幕亚洲 | 久久国产精品久久精 | 亚洲国产精品久久久久久网站 | 国产在线观看第一页 | 另类视频一区 | 久久亚洲欧美成人精品 | 国产三级香港三韩国三级 | 精品成人网 | 国产一级二级三级视频 | 性色tv| 欧美大片无尺码在线观看 | 爽死你个放荡粗暴小淫货双女视频 | 日本黄网在线观看 | 美女又黄又免费 | 欧美在线一级精品 | 久久精品国产精品亚洲人人 | 日本国产精品 | 国产精品亚洲一区二区三区在线观看 | 91久久在线| 国产91网址| 黄色美女网站免费看 | 亚洲欧洲日产国码二区首页 | a毛片在线还看免费网站 | 国产成人亚洲精品 | 成人毛片网站 |