Spring事務(wù)失效的場景梳理總結(jié)
概述
Spring針對Java Transaction API (JTA)、JDBC、Hibernate和Java Persistence API(JPA)等事務(wù) API,實現(xiàn)了一致的編程模型,而Spring的聲明式事務(wù)功能更是提供了極其方便的事務(wù)配置方式,配合Spring Boot的自動配置,大多數(shù)Spring Boot項目只需要在方法上標(biāo)記@Transactional
注解,即可一鍵開啟方法的事務(wù)性配置。
但是,事務(wù)如果沒有被正確使用,很有可能會導(dǎo)致事務(wù)的失效,帶來意想不到的數(shù)據(jù)不一致問題,隨后就是大量的人工查找問題和修復(fù)數(shù)據(jù),本次主要分享Spring事務(wù)在技術(shù)上的正確使用方式,避免因為事務(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)建一個新的事務(wù), 原來的掛起, 新的執(zhí)行完畢后, 繼續(xù)執(zhí)行老的事務(wù)
@Transactional(propagation=Propagation.REQUIRES_NEW)//必須在一個已有的事務(wù)中執(zhí)行, 否則拋出異常
@Transactional(propagation=Propagation.MANDATORY)//必須在一個沒有的事務(wù)中執(zhí)行, 否則拋出異常(與Propagation.MANDATORY相反)
@Transactional(propagation=Propagation.NEVER)//如果其他bean調(diào)用這個方法, 在其他bean中聲明事務(wù), 那就用事務(wù), 如果其他bean沒有聲明事務(wù), 那就不用事務(wù)
@Transactional(propagation=Propagation.SUPPORTS)
事務(wù)隔離級別
// 讀未提交(會出現(xiàn)臟讀, 不可重復(fù)讀) 基本不使用
@Transactional(isolation = Isolation.READ_UNCOMMITTED)// 讀已提交(會出現(xiàn)不可重復(fù)讀和幻讀) Oracle默認(rèn)
@Transactional(isolation = Isolation.READ_COMMITTED)// 可重復(fù)讀(會出現(xiàn)幻讀) MySQL默認(rèn)
@Transactional(isolation = Isolation.REPEATABLE_READ)// 串行化
@Transactional(isolation = Isolation.SERIALIZABLE)
事務(wù)失效的場景
- 事務(wù)方法未被Spring管理
如果事務(wù)方法所在的類沒有注冊到Spring IOC容器中,也就是說,事務(wù)方法所在類并沒有被Spring管理,則Spring事務(wù)會失效,舉個例子??:
public class BackGroupServiceImpl { @Autowired private SelfHelpBackgroundMapper backgroundMapper; @Transactional(propagation = Propagation.REQUIRES_NEW) public void updateSelfHelpBackground(SelfHelpBackground background) { backgroundMapper.updateByPrimaryKey(background); } }
BackGroupServiceImpl 實現(xiàn)類上沒有添加 @Service注解,實例也就沒有被加載到Spring IOC容器,此時updateSelfHelpBackground()方法的事務(wù)就會在Spring中失效。
- 同一個類中的事務(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()
會在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ù)同樣會在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ù)肯定會失效的,例如??:使用MySQL數(shù)據(jù)庫,選用MyISAM
存儲引擎,因為MyISAM
存儲引擎本身不支持事務(wù),因此事務(wù)毫無疑問會失效
- 未配置開啟事務(wù)
如果項目中沒有配置Spring的事務(wù)管理器,即使使用了Spring的事務(wù)管理功能,Spring的事務(wù)也不會生效,例如,如果你是Spring Boot項目,沒有在SpringBoot項目中配置如下代碼:
@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()
是另起了一個線程調(diào)用的。
這樣會導(dǎo)致兩個方法不在同一個線程中,從而是兩個不同的事務(wù)。如果是sendSms()
方法中拋了異常,orderCommit()
方法也回滾是不可能的。
實際上,Spring的事務(wù)是通過ThreadLocal來保證線程安全的,事務(wù)和當(dāng)前線程綁定,多個線程自然會讓事務(wù)失效。
到此這篇關(guān)于Spring事務(wù)失效的場景梳理總結(jié)的文章就介紹到這了,更多相關(guān)Spring事務(wù)失效場景內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+SpringSecurity處理Ajax登錄請求問題(推薦)
這篇文章主要介紹了SpringBoot+SpringSecurity處理Ajax登錄請求問題,本文給大家介紹的非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-12-12Spring Boot支持Crontab任務(wù)改造的方法
這篇文章主要介紹了Spring Boot支持Crontab任務(wù)改造的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01Java中Random簡介_動力節(jié)點(diǎn)Java學(xué)院整理
本文詳細(xì)給大家介紹了Java中Random簡介相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-06-06Spring?MVC策略模式之MethodArgumentResolver源碼解析
這篇文章主要為大家介紹了Spring?MVC策略模式之MethodArgumentResolver源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03