Spring事務失效的場景梳理總結(jié)
概述
Spring針對Java Transaction API (JTA)、JDBC、Hibernate和Java Persistence API(JPA)等事務 API,實現(xiàn)了一致的編程模型,而Spring的聲明式事務功能更是提供了極其方便的事務配置方式,配合Spring Boot的自動配置,大多數(shù)Spring Boot項目只需要在方法上標記@Transactional注解,即可一鍵開啟方法的事務性配置。
但是,事務如果沒有被正確使用,很有可能會導致事務的失效,帶來意想不到的數(shù)據(jù)不一致問題,隨后就是大量的人工查找問題和修復數(shù)據(jù),本次主要分享Spring事務在技術(shù)上的正確使用方式,避免因為事務處理不當導致業(yè)務邏輯產(chǎn)生大量偶發(fā)性BUG。
事務的傳播類型
//如果沒有事務就進行創(chuàng)建,存在則加入
@Transactional(propagation=Propagation.REQUIRED)//不為當前方法開啟事務
@Transactional(propagation=Propagation.NOT_SUPPORTED)//不管是否存在事務, 都創(chuàng)建一個新的事務, 原來的掛起, 新的執(zhí)行完畢后, 繼續(xù)執(zhí)行老的事務
@Transactional(propagation=Propagation.REQUIRES_NEW)//必須在一個已有的事務中執(zhí)行, 否則拋出異常
@Transactional(propagation=Propagation.MANDATORY)//必須在一個沒有的事務中執(zhí)行, 否則拋出異常(與Propagation.MANDATORY相反)
@Transactional(propagation=Propagation.NEVER)//如果其他bean調(diào)用這個方法, 在其他bean中聲明事務, 那就用事務, 如果其他bean沒有聲明事務, 那就不用事務
@Transactional(propagation=Propagation.SUPPORTS)
事務隔離級別
// 讀未提交(會出現(xiàn)臟讀, 不可重復讀) 基本不使用
@Transactional(isolation = Isolation.READ_UNCOMMITTED)// 讀已提交(會出現(xiàn)不可重復讀和幻讀) Oracle默認
@Transactional(isolation = Isolation.READ_COMMITTED)// 可重復讀(會出現(xiàn)幻讀) MySQL默認
@Transactional(isolation = Isolation.REPEATABLE_READ)// 串行化
@Transactional(isolation = Isolation.SERIALIZABLE)
事務失效的場景
- 事務方法未被Spring管理
如果事務方法所在的類沒有注冊到Spring IOC容器中,也就是說,事務方法所在類并沒有被Spring管理,則Spring事務會失效,舉個例子??:
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()方法的事務就會在Spring中失效。
- 同一個類中的事務方法被非事務方法調(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()方法沒有添加事務注解,updateSelfHelpBackgroup()方法雖然添加了事務注解,這種情況updateSelfHelpBackgroup()會在Spring事務中失效。
- 方法的事務傳播類型不支持事務
@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)部方法的事務傳播類型為不支持事務的傳播類型,則內(nèi)部方法的事務同樣會在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);
}
/**
* 事務類型聲明為NOT_SUPPORTED不支持事務的傳播
*/
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void updateProductStockById(Integer num, Long productId) {
try {
productMapper.updateProductStockById(num, productId);
} catch (Exception e) {
// 這里僅僅是捕獲異常之后的打?。ㄏ喈斢诔绦蛲痰袅水惓#?
log.error("Error updating product Stock: {}", e);
}
}
}- 數(shù)據(jù)庫不支持事務
Spring事務生效的前提是連接的數(shù)據(jù)庫支持事務,如果底層的數(shù)據(jù)庫都不支持事務,則Spring事務肯定會失效的,例如??:使用MySQL數(shù)據(jù)庫,選用MyISAM存儲引擎,因為MyISAM存儲引擎本身不支持事務,因此事務毫無疑問會失效
- 未配置開啟事務
如果項目中沒有配置Spring的事務管理器,即使使用了Spring的事務管理功能,Spring的事務也不會生效,例如,如果你是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ā)送短信
}
}通過示例,我們可以看到訂單提交的事務方法orderCommit()中,調(diào)用了發(fā)送短信的事務方法sendSms(),但是發(fā)送短信的事務方法sendSms()是另起了一個線程調(diào)用的。
這樣會導致兩個方法不在同一個線程中,從而是兩個不同的事務。如果是sendSms()方法中拋了異常,orderCommit()方法也回滾是不可能的。
實際上,Spring的事務是通過ThreadLocal來保證線程安全的,事務和當前線程綁定,多個線程自然會讓事務失效。
到此這篇關(guān)于Spring事務失效的場景梳理總結(jié)的文章就介紹到這了,更多相關(guān)Spring事務失效場景內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot+SpringSecurity處理Ajax登錄請求問題(推薦)
這篇文章主要介紹了SpringBoot+SpringSecurity處理Ajax登錄請求問題,本文給大家介紹的非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-12-12
Java中Random簡介_動力節(jié)點Java學院整理
本文詳細給大家介紹了Java中Random簡介相關(guān)知識,非常不錯,具有參考借鑒價值,需要的朋友參考下吧2017-06-06
Spring?MVC策略模式之MethodArgumentResolver源碼解析
這篇文章主要為大家介紹了Spring?MVC策略模式之MethodArgumentResolver源碼解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03

