自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

聊聊Spring事務(wù)失效的12種場景,太坑了

開發(fā) 架構(gòu)
在某些業(yè)務(wù)場景下,如果一個請求中,需要同時寫入多張表的數(shù)據(jù)。為了保證操作的原子性(要么同時成功,要么同時失敗),避免數(shù)據(jù)不一致的情況,我們一般都會用到spring事務(wù)。

[[421644]]

前言

對于從事java開發(fā)工作的同學(xué)來說,spring的事務(wù)肯定再熟悉不過了。

在某些業(yè)務(wù)場景下,如果一個請求中,需要同時寫入多張表的數(shù)據(jù)。為了保證操作的原子性(要么同時成功,要么同時失敗),避免數(shù)據(jù)不一致的情況,我們一般都會用到spring事務(wù)。

確實(shí),spring事務(wù)用起來賊爽,就用一個簡單的注解:@Transactional,就能輕松搞定事務(wù)。我猜大部分小伙伴也是這樣用的,而且一直用一直爽。

但如果你使用不當(dāng),它也會坑你于無形。

今天我們就一起聊聊,事務(wù)失效的一些場景,說不定你已經(jīng)中招了。不信,讓我們一起看看。

一 事務(wù)不生效

1.訪問權(quán)限問題

眾所周知,java的訪問權(quán)限主要有四種:private、default、protected、public,它們的權(quán)限從左到右,依次變大。

但如果我們在開發(fā)過程中,把有某些事務(wù)方法,定義了錯誤的訪問權(quán)限,就會導(dǎo)致事務(wù)功能出問題,例如:

  1. @Service 
  2. public class UserService { 
  3.      
  4.     @Transactional 
  5.     private void add(UserModel userModel) { 
  6.          saveData(userModel); 
  7.          updateData(userModel); 
  8.     } 

我們可以看到add方法的訪問權(quán)限被定義成了private,這樣會導(dǎo)致事務(wù)失效,spring要求被代理方法必須是public的。

說白了,在AbstractFallbackTransactionAttributeSource類的computeTransactionAttribute方法中有個判斷,如果目標(biāo)方法不是public,則TransactionAttribute返回null,即不支持事務(wù)。

  1. protected TransactionAttribute computeTransactionAttribute(Method method, @Nullable Class<?> targetClass) { 
  2.     // Don't allow no-public methods as required. 
  3.     if (allowPublicMethodsOnly() && !Modifier.isPublic(method.getModifiers())) { 
  4.       return null
  5.     } 
  6.  
  7.     // The method may be on an interface, but we need attributes from the target class. 
  8.     // If the target class is null, the method will be unchanged. 
  9.     Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass); 
  10.  
  11.     // First try is the method in the target class. 
  12.     TransactionAttribute txAttr = findTransactionAttribute(specificMethod); 
  13.     if (txAttr != null) { 
  14.       return txAttr; 
  15.     } 
  16.  
  17.     // Second try is the transaction attribute on the target class. 
  18.     txAttr = findTransactionAttribute(specificMethod.getDeclaringClass()); 
  19.     if (txAttr != null && ClassUtils.isUserLevelMethod(method)) { 
  20.       return txAttr; 
  21.     } 
  22.  
  23.     if (specificMethod != method) { 
  24.       // Fallback is to look at the original method. 
  25.       txAttr = findTransactionAttribute(method); 
  26.       if (txAttr != null) { 
  27.         return txAttr; 
  28.       } 
  29.       // Last fallback is the class of the original method. 
  30.       txAttr = findTransactionAttribute(method.getDeclaringClass()); 
  31.       if (txAttr != null && ClassUtils.isUserLevelMethod(method)) { 
  32.         return txAttr; 
  33.       } 
  34.     } 
  35.     return null
  36.   } 

也就是說,如果我們自定義的事務(wù)方法(即目標(biāo)方法),它的訪問權(quán)限不是public,而是private、default或protected的話,spring則不會提供事務(wù)功能。

2. 方法用final修飾

有時候,某個方法不想被子類重新,這時可以將該方法定義成final的。普通方法這樣定義是沒問題的,但如果將事務(wù)方法定義成final,例如:

  1. @Service 
  2. public class UserService { 
  3.  
  4.     @Transactional 
  5.     public final void add(UserModel userModel){ 
  6.         saveData(userModel); 
  7.         updateData(userModel); 
  8.     } 

我們可以看到add方法被定義成了final的,這樣會導(dǎo)致事務(wù)失效。

為什么?

如果你看過spring事務(wù)的源碼,可能會知道spring事務(wù)底層使用了aop,也就是通過jdk動態(tài)代理或者cglib,幫我們生成了代理類,在代理類中實(shí)現(xiàn)的事務(wù)功能。

但如果某個方法用final修飾了,那么在它的代理類中,就無法重寫該方法,而添加事務(wù)功能。

注意:如果某個方法是static的,同樣無法通過動態(tài)代理,變成事務(wù)方法。

3.方法內(nèi)部調(diào)用

有時候我們需要在某個Service類的某個方法中,調(diào)用另外一個事務(wù)方法,比如:

  1. @Service 
  2. public class UserService { 
  3.  
  4.     @Autowired 
  5.     private UserMapper userMapper; 
  6.  
  7.     @Transactional 
  8.     public void add(UserModel userModel) { 
  9.         userMapper.insertUser(userModel); 
  10.         updateStatus(userModel); 
  11.     } 
  12.  
  13.     @Transactional 
  14.     public void updateStatus(UserModel userModel) { 
  15.         doSameThing(); 
  16.     } 

我們看到在事務(wù)方法add中,直接調(diào)用事務(wù)方法updateStatus。從前面介紹的內(nèi)容可以知道,updateStatus方法擁有事務(wù)的能力是因?yàn)閟pring aop生成代理了對象,但是這種方法直接調(diào)用了this對象的方法,所以updateStatus方法不會生成事務(wù)。

由此可見,在同一個類中的方法直接內(nèi)部調(diào)用,會導(dǎo)致事務(wù)失效。

那么問題來了,如果有些場景,確實(shí)想在同一個類的某個方法中,調(diào)用它自己的另外一個方法,該怎么辦呢?

3.1 新加一個Service方法

這個方法非常簡單,只需要新加一個Service方法,把@Transactional注解加到新Service方法上,把需要事務(wù)執(zhí)行的代碼移到新方法中。具體代碼如下:

  1. @Servcie 
  2. public class ServiceA { 
  3.    @Autowired 
  4.    prvate ServiceB serviceB; 
  5.  
  6.    public void save(User user) { 
  7.          queryData1(); 
  8.          queryData2(); 
  9.          serviceB.doSave(user); 
  10.    } 
  11.  } 
  12.  
  13.  @Servcie 
  14.  public class ServiceB { 
  15.  
  16.     @Transactional(rollbackFor=Exception.class) 
  17.     public void doSave(User user) { 
  18.        addData1(); 
  19.        updateData2(); 
  20.     } 
  21.  
  22.  } 

3.2 在該Service類中注入自己

如果不想再新加一個Service類,在該Service類中注入自己也是一種選擇。具體代碼如下:

  1. @Servcie 
  2. public class ServiceA { 
  3.    @Autowired 
  4.    prvate ServiceA serviceA; 
  5.  
  6.    public void save(User user) { 
  7.          queryData1(); 
  8.          queryData2(); 
  9.          serviceA.doSave(user); 
  10.    } 
  11.  
  12.    @Transactional(rollbackFor=Exception.class) 
  13.    public void doSave(User user) { 
  14.        addData1(); 
  15.        updateData2(); 
  16.     } 
  17.  } 

可能有些人可能會有這樣的疑問:這種做法會不會出現(xiàn)循環(huán)依賴問題?

答案:不會。

其實(shí)spring ioc內(nèi)部的三級緩存保證了它,不會出現(xiàn)循環(huán)依賴問題。但有些坑,如果你想進(jìn)一步了解循環(huán)依賴問題,可以看看我之前文章《spring:我是如何解決循環(huán)依賴的?》。

3.3 通過AopContent類

在該Service類中使用AopContext.currentProxy()獲取代理對象

上面的方法2確實(shí)可以解決問題,但是代碼看起來并不直觀,還可以通過在該Service類中使用AOPProxy獲取代理對象,實(shí)現(xiàn)相同的功能。具體代碼如下:

  1. @Servcie 
  2. public class ServiceA { 
  3.  
  4.    public void save(User user) { 
  5.          queryData1(); 
  6.          queryData2(); 
  7.          ((ServiceA)AopContext.currentProxy()).doSave(user); 
  8.    } 
  9.  
  10.    @Transactional(rollbackFor=Exception.class) 
  11.    public void doSave(User user) { 
  12.        addData1(); 
  13.        updateData2(); 
  14.     } 
  15.  } 

4.未被spring管理

在我們平時開發(fā)過程中,有個細(xì)節(jié)很容易被忽略。即使用spring事務(wù)的前提是:對象要被spring管理,需要創(chuàng)建bean實(shí)例。

通常情況下,我們通過@Controller、@Service、@Component、@Repository等注解,可以自動實(shí)現(xiàn)bean實(shí)例化和依賴注入的功能。

當(dāng)然創(chuàng)建bean實(shí)例的方法還有很多,有興趣的小伙伴可以看看我之前寫的另一篇文章《@Autowired的這些騷操作,你都知道嗎?》

如果有一天,你匆匆忙忙的開發(fā)了一個Service類,但忘了加@Service注解,比如:

  1. //@Service 
  2. public class UserService { 
  3.  
  4.     @Transactional 
  5.     public void add(UserModel userModel) { 
  6.          saveData(userModel); 
  7.          updateData(userModel); 
  8.     }     

從上面的例子,我們可以看到UserService類沒有加@Service注解,那么該類不會交給spring管理,所以它的add方法也不會生成事務(wù)。

5.多線程調(diào)用

在實(shí)際項(xiàng)目開發(fā)中,多線程的使用場景還是挺多的。如果spring事務(wù)用在多線程場景中,會有問題嗎?

  1. @Slf4j 
  2. @Service 
  3. public class UserService { 
  4.  
  5.     @Autowired 
  6.     private UserMapper userMapper; 
  7.     @Autowired 
  8.     private RoleService roleService; 
  9.  
  10.     @Transactional 
  11.     public void add(UserModel userModel) throws Exception { 
  12.         userMapper.insertUser(userModel); 
  13.         new Thread(() -> { 
  14.             roleService.doOtherThing(); 
  15.         }).start(); 
  16.     } 
  17.  
  18. @Service 
  19. public class RoleService { 
  20.  
  21.     @Transactional 
  22.     public void doOtherThing() { 
  23.         System.out.println("保存role表數(shù)據(jù)"); 
  24.     } 

從上面的例子中,我們可以看到事務(wù)方法add中,調(diào)用了事務(wù)方法doOtherThing,但是事務(wù)方法doOtherThing是在另外一個線程中調(diào)用的。

這樣會導(dǎo)致兩個方法不在同一個線程中,獲取到的數(shù)據(jù)庫連接不一樣,從而是兩個不同的事務(wù)。如果想doOtherThing方法中拋了異常,add方法也回滾是不可能的。

如果看過spring事務(wù)源碼的朋友,可能會知道spring的事務(wù)是通過數(shù)據(jù)庫連接來實(shí)現(xiàn)的。當(dāng)前線程中保存了一個map,key是數(shù)據(jù)源,value是數(shù)據(jù)庫連接。

  1. private static final ThreadLocal<Map<Object, Object>> resources = 
  2.  
  3.   new NamedThreadLocal<>("Transactional resources"); 

我們說的同一個事務(wù),其實(shí)是指同一個數(shù)據(jù)庫連接,只有擁有同一個數(shù)據(jù)庫連接才能同時提交和回滾。如果在不同的線程,拿到的數(shù)據(jù)庫連接肯定是不一樣的,所以是不同的事務(wù)。

6.表不支持事務(wù)

周所周知,在mysql5之前,默認(rèn)的數(shù)據(jù)庫引擎是myisam。

它的好處就不用多說了:索引文件和數(shù)據(jù)文件是分開存儲的,對于查多寫少的單表操作,性能比innodb更好。

有些老項(xiàng)目中,可能還在用它。

在創(chuàng)建表的時候,只需要把ENGINE參數(shù)設(shè)置成MyISAM即可:

  1. CREATE TABLE `category` ( 
  2.   `id` bigint NOT NULL AUTO_INCREMENT, 
  3.   `one_category` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL
  4.   `two_category` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL
  5.   `three_category` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL
  6.   `four_category` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL
  7.   PRIMARY KEY (`id`) 
  8. ) ENGINE=MyISAM AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin 

myisam好用,但有個很致命的問題是:不支持事務(wù)。

如果只是單表操作還好,不會出現(xiàn)太大的問題。但如果需要跨多張表操作,由于其不支持事務(wù),數(shù)據(jù)極有可能會出現(xiàn)不完整的情況。

此外,myisam還不支持行鎖和外鍵。

所以在實(shí)際業(yè)務(wù)場景中,myisam使用的并不多。在mysql5以后,myisam已經(jīng)逐漸退出了歷史的舞臺,取而代之的是innodb。

有時候我們在開發(fā)的過程中,發(fā)現(xiàn)某張表的事務(wù)一直都沒有生效,那不一定是spring事務(wù)的鍋,最好確認(rèn)一下你使用的那張表,是否支持事務(wù)。

7.未開啟事務(wù)

有時候,事務(wù)沒有生效的根本原因是沒有開啟事務(wù)。

你看到這句話可能會覺得好笑。

開啟事務(wù)不是一個項(xiàng)目中,最最最基本的功能嗎?

為什么還會沒有開啟事務(wù)?

沒錯,如果項(xiàng)目已經(jīng)搭建好了,事務(wù)功能肯定是有的。

但如果你是在搭建項(xiàng)目demo的時候,只有一張表,而這張表的事務(wù)沒有生效。那么會是什么原因造成的呢?

當(dāng)然原因有很多,但沒有開啟事務(wù),這個原因極其容易被忽略。

如果你使用的是springboot項(xiàng)目,那么你很幸運(yùn)。因?yàn)閟pringboot通過DataSourceTransactionManagerAutoConfiguration類,已經(jīng)默默的幫你開啟了事務(wù)。

你所要做的事情很簡單,只需要配置spring.datasource相關(guān)參數(shù)即可。

但如果你使用的還是傳統(tǒng)的spring項(xiàng)目,則需要在applicationContext.xml文件中,手動配置事務(wù)相關(guān)參數(shù)。如果忘了配置,事務(wù)肯定是不會生效的。

具體配置如下信息:

  1.     
  2. <!-- 配置事務(wù)管理器 -->  
  3. <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">  
  4.     <property name="dataSource" ref="dataSource"></property>  
  5. </bean>  
  6. <tx:advice id="advice" transaction-manager="transactionManager">  
  7.     <tx:attributes>  
  8.         <tx:method name="*" propagation="REQUIRED"/> 
  9.     </tx:attributes>  
  10. </tx:advice>  
  11. <!-- 用切點(diǎn)把事務(wù)切進(jìn)去 -->  
  12. <aop:config>  
  13.     <aop:pointcut expression="execution(* com.susan.*.*(..))" id="pointcut"/>  
  14.     <aop:advisor advice-ref="advice" pointcut-ref="pointcut"/>  
  15. </aop:config>  

 

 

 

 

默默的說一句,如果在pointcut標(biāo)簽中的切入點(diǎn)匹配規(guī)則,配錯了的話,有些類的事務(wù)也不會生效。

二 事務(wù)不回滾

1.錯誤的傳播特性

其實(shí),我們在使用@Transactional注解時,是可以指定propagation參數(shù)的。

該參數(shù)的作用是指定事務(wù)的傳播特性,spring目前支持7種傳播特性:

  • REQUIRED 如果當(dāng)前上下文中存在事務(wù),那么加入該事務(wù),如果不存在事務(wù),創(chuàng)建一個事務(wù),這是默認(rèn)的傳播屬性值。
  • SUPPORTS 如果當(dāng)前上下文存在事務(wù),則支持事務(wù)加入事務(wù),如果不存在事務(wù),則使用非事務(wù)的方式執(zhí)行。
  • MANDATORY 如果當(dāng)前上下文中存在事務(wù),否則拋出異常。
  • REQUIRES_NEW 每次都會新建一個事務(wù),并且同時將上下文中的事務(wù)掛起,執(zhí)行當(dāng)前新建事務(wù)完成以后,上下文事務(wù)恢復(fù)再執(zhí)行。
  • NOT_SUPPORTED 如果當(dāng)前上下文中存在事務(wù),則掛起當(dāng)前事務(wù),然后新的方法在沒有事務(wù)的環(huán)境中執(zhí)行。
  • NEVER 如果當(dāng)前上下文中存在事務(wù),則拋出異常,否則在無事務(wù)環(huán)境上執(zhí)行代碼。
  • NESTED 如果當(dāng)前上下文中存在事務(wù),則嵌套事務(wù)執(zhí)行,如果不存在事務(wù),則新建事務(wù)。

如果我們在手動設(shè)置propagation參數(shù)的時候,把傳播特性設(shè)置錯了,比如:

  1. @Service 
  2. public class UserService { 
  3.  
  4.     @Transactional(propagation = Propagation.NEVER) 
  5.     public void add(UserModel userModel) { 
  6.         saveData(userModel); 
  7.         updateData(userModel); 
  8.     } 

我們可以看到add方法的事務(wù)傳播特性定義成了Propagation.NEVER,這種類型的傳播特性不支持事務(wù),如果有事務(wù)則會拋異常。

目前只有這三種傳播特性才會創(chuàng)建新事務(wù):REQUIRED,REQUIRES_NEW,NESTED。

2.自己吞了異常

事務(wù)不會回滾,最常見的問題是:開發(fā)者在代碼中手動try...catch了異常。比如:

  1. @Slf4j 
  2. @Service 
  3. public class UserService { 
  4.      
  5.     @Transactional 
  6.     public void add(UserModel userModel) { 
  7.         try { 
  8.             saveData(userModel); 
  9.             updateData(userModel); 
  10.         } catch (Exception e) { 
  11.             log.error(e.getMessage(), e); 
  12.         } 
  13.     } 

這種情況下spring事務(wù)當(dāng)然不會回滾,因?yàn)殚_發(fā)者自己捕獲了異常,又沒有手動拋出,換句話說就是把異常吞掉了。

如果想要spring事務(wù)能夠正?;貪L,必須拋出它能夠處理的異常。如果沒有拋異常,則spring認(rèn)為程序是正常的。

3.手動拋了別的異常

即使開發(fā)者沒有手動捕獲異常,但如果拋的異常不正確,spring事務(wù)也不會回滾。

  1. @Slf4j 
  2. @Service 
  3. public class UserService { 
  4.      
  5.     @Transactional 
  6.     public void add(UserModel userModel) throws Exception { 
  7.         try { 
  8.              saveData(userModel); 
  9.              updateData(userModel); 
  10.         } catch (Exception e) { 
  11.             log.error(e.getMessage(), e); 
  12.             throw new Exception(e); 
  13.         } 
  14.     } 

上面的這種情況,開發(fā)人員自己捕獲了異常,又手動拋出了異常:Exception,事務(wù)同樣不會回滾。

因?yàn)閟pring事務(wù),默認(rèn)情況下只會回滾RuntimeException(運(yùn)行時異常)和Error(錯誤),對于普通的Exception(非運(yùn)行時異常),它不會回滾。

4.自定義了回滾異常

在使用@Transactional注解聲明事務(wù)時,有時我們想自定義回滾的異常,spring也是支持的??梢酝ㄟ^設(shè)置rollbackFor參數(shù),來完成這個功能。

但如果這個參數(shù)的值設(shè)置錯了,就會引出一些莫名其妙的問題,例如:

  1. @Slf4j 
  2. @Service 
  3. public class UserService { 
  4.      
  5.     @Transactional(rollbackFor = BusinessException.class) 
  6.     public void add(UserModel userModel) throws Exception { 
  7.        saveData(userModel); 
  8.        updateData(userModel); 
  9.     } 

如果在執(zhí)行上面這段代碼,保存和更新數(shù)據(jù)時,程序報錯了,拋了SqlException、DuplicateKeyException等異常。而BusinessException是我們自定義的異常,報錯的異常不屬于BusinessException,所以事務(wù)也不會回滾。

即使rollbackFor有默認(rèn)值,但阿里巴巴開發(fā)者規(guī)范中,還是要求開發(fā)者重新指定該參數(shù)。

這是為什么呢?

因?yàn)槿绻褂媚J(rèn)值,一旦程序拋出了Exception,事務(wù)不會回滾,這會出現(xiàn)很大的bug。所以,建議一般情況下,將該參數(shù)設(shè)置成:Exception或Throwable。

5.嵌套事務(wù)回滾多了

  1. public class UserService { 
  2.  
  3.     @Autowired 
  4.     private UserMapper userMapper; 
  5.  
  6.     @Autowired 
  7.     private RoleService roleService; 
  8.  
  9.     @Transactional 
  10.     public void add(UserModel userModel) throws Exception { 
  11.         userMapper.insertUser(userModel); 
  12.         roleService.doOtherThing(); 
  13.     } 
  14.  
  15. @Service 
  16. public class RoleService { 
  17.  
  18.     @Transactional(propagation = Propagation.NESTED) 
  19.     public void doOtherThing() { 
  20.         System.out.println("保存role表數(shù)據(jù)"); 
  21.     } 

這種情況使用了嵌套的內(nèi)部事務(wù),原本是希望調(diào)用roleService.doOtherThing方法時,如果出現(xiàn)了異常,只回滾doOtherThing方法里的內(nèi)容,不回滾 userMapper.insertUser里的內(nèi)容,即回滾保存點(diǎn)。。但事實(shí)是,insertUser也回滾了。

why?

因?yàn)閐oOtherThing方法出現(xiàn)了異常,沒有手動捕獲,會繼續(xù)往上拋,到外層add方法的代理方法中捕獲了異常。所以,這種情況是直接回滾了整個事務(wù),不只回滾單個保存點(diǎn)。

怎么樣才能只回滾保存點(diǎn)呢?

  1. @Slf4j 
  2. @Service 
  3. public class UserService { 
  4.  
  5.     @Autowired 
  6.     private UserMapper userMapper; 
  7.  
  8.     @Autowired 
  9.     private RoleService roleService; 
  10.  
  11.     @Transactional 
  12.     public void add(UserModel userModel) throws Exception { 
  13.  
  14.         userMapper.insertUser(userModel); 
  15.         try { 
  16.             roleService.doOtherThing(); 
  17.         } catch (Exception e) { 
  18.             log.error(e.getMessage(), e); 
  19.         } 
  20.     } 

可以將內(nèi)部嵌套事務(wù)放在try/catch中,并且不繼續(xù)往上拋異常。這樣就能保證,如果內(nèi)部嵌套事務(wù)中出現(xiàn)異常,只回滾內(nèi)部事務(wù),而不影響外部事務(wù)。

三 其他

1 大事務(wù)問題

在使用spring事務(wù)時,有個讓人非常頭疼的問題,就是大事務(wù)問題。

通常情況下,我們會在方法上@Transactional注解,填加事務(wù)功能,比如:

  1. @Service 
  2. public class UserService { 
  3.      
  4.     @Autowired  
  5.     private RoleService roleService; 
  6.      
  7.     @Transactional 
  8.     public void add(UserModel userModel) throws Exception { 
  9.        query1(); 
  10.        query2(); 
  11.        query3(); 
  12.        roleService.save(userModel); 
  13.        update(userModel); 
  14.     } 
  15.  
  16.  
  17. @Service 
  18. public class RoleService { 
  19.      
  20.     @Autowired  
  21.     private RoleService roleService; 
  22.      
  23.     @Transactional 
  24.     public void save(UserModel userModel) throws Exception { 
  25.        query4(); 
  26.        query5(); 
  27.        query6(); 
  28.        saveData(userModel); 
  29.     } 

但@Transactional注解,如果被加到方法上,有個缺點(diǎn)就是整個方法都包含在事務(wù)當(dāng)中了。

上面的這個例子中,在UserService類中,其實(shí)只有這兩行才需要事務(wù):

  1. roleService.save(userModel); 
  2. update(userModel); 

在RoleService類中,只有這一行需要事務(wù):

  1. saveData(userModel); 

現(xiàn)在的這種寫法,會導(dǎo)致所有的query方法也被包含在同一個事務(wù)當(dāng)中。

如果query方法非常多,調(diào)用層級很深,而且有部分查詢方法比較耗時的話,會造成整個事務(wù)非常耗時,而從造成大事務(wù)問題。

關(guān)于大事務(wù)問題的危害,可以閱讀一下我的另一篇文章《讓人頭痛的大事務(wù)問題到底要如何解決?》,上面有詳細(xì)的講解。

2.編程式事務(wù)

上面聊的這些內(nèi)容都是基于@Transactional注解的,主要說的是它的事務(wù)問題,我們把這種事務(wù)叫做:聲明式事務(wù)。

其實(shí),spring還提供了另外一種創(chuàng)建事務(wù)的方式,即通過手動編寫代碼實(shí)現(xiàn)的事務(wù),我們把這種事務(wù)叫做:編程式事務(wù)。例如:

  1. @Autowired 
  2. private TransactionTemplate transactionTemplate; 
  3.  
  4. ... 
  5.  
  6. public void save(final User user) { 
  7.       queryData1(); 
  8.       queryData2(); 
  9.       transactionTemplate.execute((status) => { 
  10.          addData1(); 
  11.          updateData2(); 
  12.          return Boolean.TRUE
  13.       }) 

在spring中為了支持編程式事務(wù),專門提供了一個類:TransactionTemplate,在它的execute方法中,就實(shí)現(xiàn)了事務(wù)的功能。

相較于@Transactional注解聲明式事務(wù),我更建議大家使用,基于TransactionTemplate的編程式事務(wù)。主要原因如下:

  • 避免由于spring aop問題,導(dǎo)致事務(wù)失效的問題。
  • 能夠更小粒度的控制事務(wù)的范圍,更直觀。

 

建議在項(xiàng)目中少使用@Transactional注解開啟事務(wù)。但并不是說一定不能用它,如果項(xiàng)目中有些業(yè)務(wù)邏輯比較簡單,而且不經(jīng)常變動,使用@Transactional注解開啟事務(wù)開啟事務(wù)也無妨,因?yàn)樗唵?,開發(fā)效率更高,但是千萬要小心事務(wù)失效的問題。

 

責(zé)任編輯:武曉燕 來源: 蘇三說技術(shù)
相關(guān)推薦

2022-01-09 18:32:03

MySQL SQL 語句數(shù)據(jù)庫

2024-09-09 08:29:25

2022-08-08 17:38:45

Spring策略事務(wù)

2022-08-09 09:34:32

Spring開發(fā)

2022-02-14 16:53:57

Spring項(xiàng)目數(shù)據(jù)庫

2023-07-05 08:45:18

Spring事務(wù)失效場景

2022-05-02 21:47:13

并發(fā)編程線程

2022-12-06 10:39:43

Spring事務(wù)失效

2022-07-05 14:19:30

Spring接口CGLIB

2022-05-26 08:23:05

MySQL索引數(shù)據(jù)庫

2024-01-29 08:28:01

Spring事務(wù)失效

2021-12-13 11:12:41

Spring事務(wù)失效

2022-10-11 11:38:23

Spring

2021-04-14 15:17:08

Transaction代碼語言

2025-02-10 00:27:54

2022-02-28 08:55:31

數(shù)據(jù)庫MySQL索引

2023-05-26 07:19:49

Spring聲明式事務(wù)

2024-05-07 08:23:03

Spring@Async配置

2023-09-28 09:07:54

注解失效場景

2022-04-13 20:53:15

Spring事務(wù)管理
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號