一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring事務失效的幾種原因

Spring事務失效的幾種原因

2020-09-16 13:56邊鋒 Java教程

在日常編碼過程中常常涉及到事務,在前兩天看到一篇文章提到了Spring事務,那么在此總結下在Spring環境下事務失效的幾種原因.

數據庫引擎不支持事務

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

@transactional加在private方法上

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

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

例如:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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中是使用代理的方式實現事務,發生自身調用的時候,沒有經過Spring的代理,自然事務失效.

不支持事務

?
1
2
3
4
5
6
7
8
9
@Service
public class UserServiceImpl implements UserService {
 
  @Transactional(propagation = Propagation.NOT_SUPPORTED)
  public void update(User user) {
  //do some action
  }
 
}

@Transactional(propagation = Propagation.NOT_SUPPORTED)表示如果當前存在事務就掛起,以沒有事務的方式運行,主動不支持事務了,那么再怎么操作也是白搭. 此處貼下Spring的傳播行為:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
/**
 * 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
@Service
public class UserServiceImpl implements UserService {
 
  @Transactional
  public void update(User user) {
  try{
 
  }catch(Exception e){
    log.error(e.getMessage(),e);
  }
  }
 
}

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

異常類型錯誤

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

沒有被Spring管理

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

沒有配置TransactionManager

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

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

原文鏈接:https://www.bianxiaofeng.com/article/77

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产一区二区三区毛片 | 国产精品嫩草影院一二三区 | 日韩ab | 四虎在线精品观看免费 | 成年人免费观看视频网站 | 成全动漫视频在线观看 | 人成午夜免费大片在线观看 | 和直男装修工在工地啪 | bt天堂在线观看国产 | 贰佰麻豆剧果冻传媒一二三区 | 成人欧美1314www色视频 | 无码射肉在线播放视频 | 韩国女主播在线大尺无遮挡 | 5g影院天天影院天天爽影院网站 | 精品欧美小视频在线观看 | 人与善交大片免费看 | 国产麻豆91欧美一区二区 | 1024日韩基地 | 四虎国产精品视频免费看 | 亚洲七七久久综合桃花 | 亚洲欧美精品一区天堂久久 | 国产欧美日韩免费一区二区 | 亚洲妇熟xxxxx妇色黄 | 狠狠燥 | 色婷婷综合久久久中文字幕 | 国产免费不卡视频 | 秋霞啪啪片 | 日本ww视频 | 91国在线观看| 四虎精品免费国产成人 | 亚洲男人天堂久久 | 国产v在线播放 | 精品一二三区久久AAA片 | 婷婷99视频精品全部在线观看 | 幸福草电视剧演员表介绍 | 男女刺激高清视频在线观看 | 精品久久久久久久久久久久久久久 | 亚洲黄色免费在线观看 | 182免费在线观看 | 日本公乱妇视频 | 成人国产网站v片免费观看 成人国产精品视频 |