SpringBoot中手動(dòng)開啟事務(wù)的實(shí)現(xiàn)方法
在Spring Boot中,雖然大多數(shù)情況下推薦使用@Transactional注解來管理事務(wù),但有時(shí)需要更靈活地手動(dòng)控制事務(wù)。這可以通過TransactionTemplate或PlatformTransactionManager來實(shí)現(xiàn)
一、使用 TransactionTemplate
TransactionTemplate是Spring提供的一個(gè)模板類,用于簡(jiǎn)化事務(wù)管理。
1、配置 TransactionTemplate
(首先,確保你的項(xiàng)目已經(jīng)配置了數(shù)據(jù)源和事務(wù)管理器(Spring Boot通常會(huì)自動(dòng)配置這些))。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;
@Configuration
public class TransactionConfig {
@Bean
public TransactionTemplate transactionTemplate(PlatformTransactionManager transactionManager) {
return new TransactionTemplate(transactionManager);
}
}2、使用 TransactionTemplate
在需要手動(dòng)管理事務(wù)的服務(wù)類中注入并使用TransactionTemplate:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
@Service
public class MyService {
@Autowired
private TransactionTemplate transactionTemplate;
public void saveData() {
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(org.springframework.transaction.TransactionStatus status) {
try {
// 偽代碼
// myRepository.save(new MyEntity("Data 1"));
// myRepository.save(new MyEntity("Data 2"));
// Simulate an exception to trigger a rollback
if (true) {
throw new RuntimeException("Simulated exception");
}
// myRepository.save(new MyEntity("Data 3"));
} catch (RuntimeException e) {
// Rollback transaction if exception occurs
status.setRollbackOnly();
throw e;
}
}
});
}
}二、使用 PlatformTransactionManager 直接管理事務(wù)
PlatformTransactionManager接口提供了更細(xì)粒度的事務(wù)控制,適用于需要復(fù)雜事務(wù)管理的場(chǎng)景。
1、注入 PlatformTransactionManager
在需要手動(dòng)管理事務(wù)的服務(wù)類中注入PlatformTransactionManager:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
@Service
public class MyService {
@Autowired
private PlatformTransactionManager transactionManager;
public void performTransactionalOperation() {
DefaultTransactionDefinition def = new DefaultTransactionDefinition();
def.setName("myTransaction");
def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
TransactionStatus status = transactionManager.getTransaction(def);
try {
// Perform database operations here
// myRepository.save(new MyEntity("Data 1"));
// myRepository.save(new MyEntity("Data 2"));
// Simulate an exception to trigger a rollback
if (true) {
throw new RuntimeException("Simulated exception");
}
// myRepository.save(new MyEntity("Data 3"));
// Commit transaction
transactionManager.commit(status);
} catch (RuntimeException e) {
transactionManager.rollback(status);
throw e;
}
}
}到此這篇關(guān)于SpringBoot中手動(dòng)開啟事務(wù)的實(shí)現(xiàn)方法的文章就介紹到這了,更多相關(guān)SpringBoot手動(dòng)開啟事務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
淺談shiro的SecurityManager類結(jié)構(gòu)
下面小編就為大家?guī)硪黄獪\談shiro的SecurityManager類結(jié)構(gòu)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-07-07
在SpringBoot項(xiàng)目中動(dòng)態(tài)切換數(shù)據(jù)源和數(shù)據(jù)庫的詳細(xì)步驟
在許多企業(yè)級(jí)應(yīng)用中,可能需要根據(jù)不同的業(yè)務(wù)需求來切換不同的數(shù)據(jù)庫,如讀寫分離、分庫分表等場(chǎng)景,Spring Boot 提供了靈活的數(shù)據(jù)源配置方式,本文將介紹如何在 Spring Boot 項(xiàng)目中實(shí)現(xiàn)動(dòng)態(tài)切換數(shù)據(jù)源和數(shù)據(jù)庫的方案,需要的朋友可以參考下2025-08-08
SpringBoot 使用 @Configuration 集中管理 Bean的實(shí)
在 SpringBoot 中,@Configuration 注解是專門用來集中管理 Bean 的核心方案,它可以替代傳統(tǒng) XML 配置文件,本文給大家介紹SpringBoot使用@Configuration集中管理 Bean的實(shí)戰(zhàn)步驟,感興趣的朋友跟隨小編一起看看吧2026-04-04
idea熱部署且開啟自動(dòng)編譯的實(shí)現(xiàn)方法
這篇文章主要介紹了idea熱部署且開啟自動(dòng)編譯的實(shí)現(xiàn)方法,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
RestTemplate報(bào)錯(cuò)400 Bad Request的解決方案
在使用Spring Boot時(shí),若直接通過@Autowired注入RestTemplate可能會(huì)遇到400BadRequest錯(cuò)誤,原因在于Spring Boot官方文檔指出,由于RestTemplate實(shí)例通常需要在使用前進(jìn)行定制,因此Spring Boot不會(huì)自動(dòng)配置單個(gè)RestTemplate Bean2024-11-11
Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼
這篇文章主要介紹了 Java 定時(shí)器(Timer,TimerTask)詳解及實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-01-01
Spring Security基于過濾器實(shí)現(xiàn)圖形驗(yàn)證碼功能
驗(yàn)證碼就是為了防止惡意用戶采用暴力重試的攻擊手段而設(shè)置的一種防護(hù)措施,接下來在Spring Security的環(huán)境中,我們可以用兩種方案實(shí)現(xiàn)圖形驗(yàn)證碼,具體實(shí)現(xiàn)方法跟隨小編一起看看吧2021-09-09

