Spring事務(wù)失效的場(chǎng)景梳理總結(jié)
概述
Spring針對(duì)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ù)的失效,帶來(lái)意想不到的數(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ù), 原來(lái)的掛起, 新的執(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ù)隔離級(jí)別
// 讀未提交(會(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ù)失效的場(chǎng)景
- 事務(wù)方法未被Spring管理
如果事務(wù)方法所在的類沒有注冊(cè)到Spring IOC容器中,也就是說(shuō),事務(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);
// 扣減庫(kù)存
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ù)庫(kù)不支持事務(wù)
Spring事務(wù)生效的前提是連接的數(shù)據(jù)庫(kù)支持事務(wù),如果底層的數(shù)據(jù)庫(kù)都不支持事務(wù),則Spring事務(wù)肯定會(huì)失效的,例如??:使用MySQL數(shù)據(jù)庫(kù),選用MyISAM存儲(chǔ)引擎,因?yàn)?code>MyISAM存儲(chǔ)引擎本身不支持事務(wù),因此事務(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ā)送短信
}
}通過(guò)示例,我們可以看到訂單提交的事務(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ù)是通過(guò)ThreadLocal來(lái)保證線程安全的,事務(wù)和當(dāng)前線程綁定,多個(gè)線程自然會(huì)讓事務(wù)失效。
到此這篇關(guān)于Spring事務(wù)失效的場(chǎng)景梳理總結(jié)的文章就介紹到這了,更多相關(guān)Spring事務(wù)失效場(chǎng)景內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Mybatis使用foreach批量更新數(shù)據(jù)報(bào)無(wú)效字符錯(cuò)誤問題
這篇文章主要介紹了Mybatis使用foreach批量更新數(shù)據(jù)報(bào)無(wú)效字符錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-08-08
Mybatis-Plus根據(jù)自定義注解實(shí)現(xiàn)自動(dòng)加解密的示例代碼
我們把數(shù)據(jù)存到數(shù)據(jù)庫(kù)的時(shí)候,有些敏感字段是需要加密的,從數(shù)據(jù)庫(kù)查出來(lái)再進(jìn)行解密,如果我們使用的是Mybatis框架,那就跟著一起探索下如何使用框架的攔截器功能實(shí)現(xiàn)自動(dòng)加解密吧,需要的朋友可以參考下2024-06-06
JAVA 字符串加密、密碼加密實(shí)現(xiàn)方法
這篇文章主要介紹了JAVA 字符串加密、密碼加密實(shí)現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2016-10-10
如何解決Could not transfer artifact org.spri
在Maven更新過(guò)程中遇到“Could not transfer artifact org.springframework.boot”錯(cuò)誤通常是由于網(wǎng)絡(luò)問題,解決方法是在Maven的設(shè)置中忽略HTTPS,添加特定語(yǔ)句后,可以正常下載依賴,但下載速度可能較慢,這是一種常見的解決方案,希望對(duì)遇到相同問題的人有所幫助2024-09-09
淺談升級(jí)Spring Cloud到Finchley后的一點(diǎn)坑
這篇文章主要介紹了淺談升級(jí)Spring Cloud到Finchley后的一點(diǎn)坑,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-10-10
springboot中事務(wù)管理@Transactional的注意事項(xiàng)與使用場(chǎng)景
今天小編就為大家分享一篇關(guān)于springboot中事務(wù)管理@Transactional的注意事項(xiàng)與使用場(chǎng)景,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04
spring cloud 之 Feign 使用HTTP請(qǐng)求遠(yuǎn)程服務(wù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇spring cloud 之 Feign 使用HTTP請(qǐng)求遠(yuǎn)程服務(wù)的實(shí)現(xiàn)方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06

