springboot手動事務(wù)回滾的實(shí)現(xiàn)代碼
親測在使用@Transactional、@Transactional(rollbackFor = Exception.class)及catch異常之后 throw new RuntimeException();仍然不能解決線程中的事務(wù)回滾。下面使用線程所機(jī)制,進(jìn)行整體的事務(wù)提交及事務(wù)回滾,代碼如下:
在springboot啟動類上加 @EnableTransactionManagement 注解
線程類中添加以下代碼
@Autowired
private PlatformTransactionManager platformTransactionManager;
@Autowired
private TransactionDefinition transactionDefinition;
private Lock lock = new ReentrantLock();
// todo 業(yè)務(wù)處理方法 數(shù)據(jù)存儲異常 手動進(jìn)行回滾
public void saveMsg(String message) throws Exception {
lock.lock();
TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition);
try {
//todo 具體業(yè)務(wù),對數(shù)據(jù)庫的操作 start
test1Service.save(test1);
test2Service.save(test2);
//end
platformTransactionManager.commit(transaction);
} catch (Exception e) {
platformTransactionManager.rollback(transaction);
e.printStackTrace();
} finally {
lock.unlock();
}
}
注:如果無法用 @Autowired 程序啟動進(jìn)行對象創(chuàng)建,可以使用init靜態(tài)注入,如果對象可以正常創(chuàng)建,下面代碼可以忽略。
@Autowired
private static PlatformTransactionManager platformTransactionManager;
@Autowired
private static TransactionDefinition transactionDefinition;
@Autowired
public void init(PlatformTransactionManager platformTransactionManager,TransactionDefinition transactionDefinition
) {
DriverAlfaServerHandler.platformTransactionManager = platformTransactionManager;
DriverAlfaServerHandler.transactionDefinition = transactionDefinition;
}
此回滾方法親測有效。
到此這篇關(guān)于springboot手動事務(wù)回滾的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)springboot 事務(wù)回滾內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳細(xì)分析Java并發(fā)集合ArrayBlockingQueue的用法
這篇文章主要介紹了詳細(xì)分析Java并發(fā)集合ArrayBlockingQueue的用法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用詳解
這篇文章主要介紹了MyBatis-Plus QueryWrapper及LambdaQueryWrapper的使用詳解,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
解決@Transactional注解事務(wù)不回滾不起作用的問題
這篇文章主要介紹了解決@Transactional注解事務(wù)不回滾不起作用的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
Mybatis調(diào)用Oracle存儲過程的方法圖文詳解
這篇文章主要介紹了Mybatis調(diào)用Oracle存儲過程的方法介紹,需要的朋友可以參考下2017-09-09
Spring Cloud Ribbon實(shí)現(xiàn)客戶端負(fù)載均衡的方法
本篇文章主要介紹了Spring Cloud Ribbon實(shí)現(xiàn)客戶端負(fù)載均衡的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05
Java中消息隊(duì)列任務(wù)的平滑關(guān)閉詳解
對于消息隊(duì)列的監(jiān)聽,我們一般使用Java寫一個(gè)獨(dú)立的程序,在Linux服務(wù)器上運(yùn)行。程序啟動后,通過消息隊(duì)列客戶端接收消息,放入一個(gè)線程池進(jìn)行異步處理,并發(fā)的快速處理。這篇文章主要給大家介紹了關(guān)于Java中消息隊(duì)列任務(wù)的平滑關(guān)閉的相關(guān)資料,需要的朋友可以參考下。2017-11-11
關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸
這篇文章主要介紹了關(guān)于springboot 中使用httpclient或RestTemplate做MultipartFile文件跨服務(wù)傳輸?shù)膯栴},本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
Java?EasyExcel利用填充模版動態(tài)生成多個(gè)sheet頁
這篇文章主要為大家詳細(xì)介紹了Java?EasyExcel如何利用填充模版動態(tài)生成多個(gè)sheet頁,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-12-12
java 四舍五入保留小數(shù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)硪黄猨ava 四舍五入保留小數(shù)的實(shí)現(xiàn)方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-09-09

