Spring事務(wù)失效的場景梳理總結(jié)
概述
Spring針對Java Transaction API (JTA)、JDBC、Hibernate和Java Persistence API(JPA)等事務(wù) API,實(shí)現(xiàn)了一致的編程模型,而Spring的聲明式事務(wù)功能更是提供了極其方便的事務(wù)配置方式,配合Spring Boot的自動(dòng)配置,大多數(shù)Spring Boot項(xiàng)目只需要在方法上標(biāo)記@Transactional
注解,即可一鍵開啟方法的事務(wù)性配置。
但是,事務(wù)如果沒有被正確使用,很有可能會(huì)導(dǎo)致事務(wù)的失效,帶來意想不到的數(shù)據(jù)不一致問題,隨后就是大量的人工查找問題和修復(fù)數(shù)據(jù),本次主要分享Spring事務(wù)在技術(shù)上的正確使用方式,避免因?yàn)槭聞?wù)處理不當(dāng)導(dǎo)致業(yè)務(wù)邏輯產(chǎn)生大量偶發(fā)性BUG。
事務(wù)的傳播類型
//如果沒有事務(wù)就進(jìn)行創(chuàng)建,存在則加入
@Transactional(propagation=Propagation.REQUIRED)//不為當(dāng)前方法開啟事務(wù)
@Transactional(propagation=Propagation.NOT_SUPPORTED)//不管是否存在事務(wù), 都創(chuàng)建一個(gè)新的事務(wù), 原來的掛起, 新的執(zhí)行完畢后, 繼續(xù)執(zhí)行老的事務(wù)
@Transactional(propagation=Propagation.REQUIRES_NEW)//必須在一個(gè)已有的事務(wù)中執(zhí)行, 否則拋出異常
@Transactional(propagation=Propagation.MANDATORY)//必須在一個(gè)沒有的事務(wù)中執(zhí)行, 否則拋出異常(與Propagation.MANDATORY相反)
@Transactional(propagation=Propagation.NEVER)//如果其他bean調(diào)用這個(gè)方法, 在其他bean中聲明事務(wù), 那就用事務(wù), 如果其他bean沒有聲明事務(wù), 那就不用事務(wù)
@Transactional(propagation=Propagation.SUPPORTS)
事務(wù)隔離級別
// 讀未提交(會(huì)出現(xiàn)臟讀, 不可重復(fù)讀) 基本不使用
@Transactional(isolation = Isolation.READ_UNCOMMITTED)// 讀已提交(會(huì)出現(xiàn)不可重復(fù)讀和幻讀) Oracle默認(rèn)
@Transactional(isolation = Isolation.READ_COMMITTED)// 可重復(fù)讀(會(huì)出現(xiàn)幻讀) MySQL默認(rèn)
@Transactional(isolation = Isolation.REPEATABLE_READ)// 串行化
@Transactional(isolation = Isolation.SERIALIZABLE)
事務(wù)失效的場景
- 事務(wù)方法未被Spring管理
如果事務(wù)方法所在的類沒有注冊到Spring IOC容器中,也就是說,事務(wù)方法所在類并沒有被Spring管理,則Spring事務(wù)會(huì)失效,舉個(gè)例子??:
public class BackGroupServiceImpl { @Autowired private SelfHelpBackgroundMapper backgroundMapper; @Transactional(propagation = Propagation.REQUIRES_NEW) public void updateSelfHelpBackground(SelfHelpBackground background) { backgroundMapper.updateByPrimaryKey(background); } }
BackGroupServiceImpl 實(shí)現(xiàn)類上沒有添加 @Service注解,實(shí)例也就沒有被加載到Spring IOC容器,此時(shí)updateSelfHelpBackground()方法的事務(wù)就會(huì)在Spring中失效。
- 同一個(gè)類中的事務(wù)方法被非事務(wù)方法調(diào)用
@Service public class BackGroupServiceImpl { @Autowired private SelfHelpBackgroundMapper backgroundMapper; @Transactional(propagation = Propagation.REQUIRED) public void updateSelfHelpBackground(SelfHelpBackground background) { backgroundMapper.updateByPrimaryKey(background); } public void updateBackground(){ updateSelfHelpBackground(new SelfHelpBackground()); } }
updateBackgroup()
方法和updateSelfHelpBackgroup()
方法都在BackGroupServiceImpl類中,然而updateBackgroup()
方法沒有添加事務(wù)注解,updateSelfHelpBackgroup()
方法雖然添加了事務(wù)注解,這種情況updateSelfHelpBackgroup()
會(huì)在Spring事務(wù)中失效。
- 方法的事務(wù)傳播類型不支持事務(wù)
@Service public class BackGroupServiceImpl { @Autowired private SelfHelpBackgroundMapper backgroundMapper; @Transactional(propagation = Propagation.REQUIRED) public void updateSelfHelpBackground(SelfHelpBackground background) { backgroundMapper.updateByPrimaryKey(background); } @Transactional(propagation = Propagation.NOT_SUPPORTED) public void updateBackground(SelfHelpBackground background){ backgroundMapper.updateByPrimaryKey(background); } }
如果內(nèi)部方法的事務(wù)傳播類型為不支持事務(wù)的傳播類型,則內(nèi)部方法的事務(wù)同樣會(huì)在Spring中失效,如@Transactional(propagation = Propagation.NOT_SUPPORTED)
- 異常被內(nèi)部catch,程序生吞異常
@Service public class OrderServiceImpl{ @Autowired private OrderMapper orderMapper; @Autowired private ProductMapper productMapper; @Override @Transactional(propagation = Propagation.REQUIRES_NEW) public ResponseEntity submitOrder(Order order) { long orderNo = Math.abs(ThreadLocalRandom.current().nextLong(1000)); order.setOrderNo("ORDER_" + orderNo); orderMapper.insert(order); // 扣減庫存 this.updateProductStockById(order.getProductId(), 1L); return new ResponseEntity(HttpStatus.OK); } /** * 事務(wù)類型聲明為NOT_SUPPORTED不支持事務(wù)的傳播 */ @Transactional(propagation = Propagation.NOT_SUPPORTED) public void updateProductStockById(Integer num, Long productId) { try { productMapper.updateProductStockById(num, productId); } catch (Exception e) { // 這里僅僅是捕獲異常之后的打?。ㄏ喈?dāng)于程序吞掉了異常) log.error("Error updating product Stock: {}", e); } } }
- 數(shù)據(jù)庫不支持事務(wù)
Spring事務(wù)生效的前提是連接的數(shù)據(jù)庫支持事務(wù),如果底層的數(shù)據(jù)庫都不支持事務(wù),則Spring事務(wù)肯定會(huì)失效的,例如??:使用MySQL數(shù)據(jù)庫,選用MyISAM
存儲(chǔ)引擎,因?yàn)?code>MyISAM存儲(chǔ)引擎本身不支持事務(wù),因此事務(wù)毫無疑問會(huì)失效
- 未配置開啟事務(wù)
如果項(xiàng)目中沒有配置Spring的事務(wù)管理器,即使使用了Spring的事務(wù)管理功能,Spring的事務(wù)也不會(huì)生效,例如,如果你是Spring Boot項(xiàng)目,沒有在SpringBoot項(xiàng)目中配置如下代碼:
@Bean public PlatformTransactionManager transactionManager(DataSource dataSource) { return new DataSourceTransactionManager(dataSource); }
- 多線程調(diào)用
@Slf4j @Service public class OrderServiceImpl { @Autowired private OrderMapper orderMapper; @Autowired private MessageService messageService; @Transactional public void orderCommit(orderModel orderModel) throws Exception { orderMapper.insertOrder(orderModel); new Thread(() -> { messageService.sendSms(); }).start(); } } @Service public class MessageService { @Transactional public void sendSms() { // 發(fā)送短信 } }
通過示例,我們可以看到訂單提交的事務(wù)方法orderCommit()
中,調(diào)用了發(fā)送短信的事務(wù)方法sendSms()
,但是發(fā)送短信的事務(wù)方法sendSms()
是另起了一個(gè)線程調(diào)用的。
這樣會(huì)導(dǎo)致兩個(gè)方法不在同一個(gè)線程中,從而是兩個(gè)不同的事務(wù)。如果是sendSms()
方法中拋了異常,orderCommit()
方法也回滾是不可能的。
實(shí)際上,Spring的事務(wù)是通過ThreadLocal來保證線程安全的,事務(wù)和當(dāng)前線程綁定,多個(gè)線程自然會(huì)讓事務(wù)失效。
到此這篇關(guān)于Spring事務(wù)失效的場景梳理總結(jié)的文章就介紹到這了,更多相關(guān)Spring事務(wù)失效場景內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+SpringSecurity處理Ajax登錄請求問題(推薦)
這篇文章主要介紹了SpringBoot+SpringSecurity處理Ajax登錄請求問題,本文給大家介紹的非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12Spring Boot支持Crontab任務(wù)改造的方法
這篇文章主要介紹了Spring Boot支持Crontab任務(wù)改造的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01教你用Java Swing做一個(gè)定時(shí)提醒工具
今天給大家?guī)淼氖荍ava的相關(guān)知識(shí),文章圍繞著如何用Java做一個(gè)定時(shí)提醒工具展開,文中有非常詳細(xì)的介紹及代碼示例,需要的朋友可以參考下2021-06-06Java中Random簡介_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
本文詳細(xì)給大家介紹了Java中Random簡介相關(guān)知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-06-06idea插件生成jpa實(shí)體類的實(shí)現(xiàn)示例
本文主要介紹了idea插件生成jpa實(shí)體類的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01IDEA修改生成jar包名字的兩種方法實(shí)現(xiàn)
本文主要介紹了IDEA修改生成jar包名字的兩種方法實(shí)現(xiàn),通過簡單的步驟,您可以修改項(xiàng)目名稱并在打包時(shí)使用新的名稱,具有一定的參考價(jià)值,感興趣的可以了解下2023-08-08Spring?MVC策略模式之MethodArgumentResolver源碼解析
這篇文章主要為大家介紹了Spring?MVC策略模式之MethodArgumentResolver源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03