springboot手動(dòng)事務(wù)回滾的實(shí)現(xiàn)代碼
親測(cè)在使用@Transactional、@Transactional(rollbackFor = Exception.class)及catch異常之后 throw new RuntimeException();仍然不能解決線程中的事務(wù)回滾。下面使用線程所機(jī)制,進(jìn)行整體的事務(wù)提交及事務(wù)回滾,代碼如下:
在springboot啟動(dòng)類上加 @EnableTransactionManagement 注解
線程類中添加以下代碼
@Autowired private PlatformTransactionManager platformTransactionManager; @Autowired private TransactionDefinition transactionDefinition; private Lock lock = new ReentrantLock(); // todo 業(yè)務(wù)處理方法 數(shù)據(jù)存儲(chǔ)異常 手動(dòng)進(jìn)行回滾 public void saveMsg(String message) throws Exception { lock.lock(); TransactionStatus transaction = platformTransactionManager.getTransaction(transactionDefinition); try { //todo 具體業(yè)務(wù),對(duì)數(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 程序啟動(dòng)進(jìn)行對(duì)象創(chuàng)建,可以使用init靜態(tài)注入,如果對(duì)象可以正常創(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; }
此回滾方法親測(cè)有效。
到此這篇關(guān)于springboot手動(dòng)事務(wù)回滾的實(shí)現(xiàn)代碼的文章就介紹到這了,更多相關(guān)springboot 事務(wù)回滾內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot項(xiàng)目啟動(dòng)的時(shí)候,運(yùn)行main方法報(bào)錯(cuò)NoClassDefFoundError問題
這篇文章主要介紹了springboot項(xiàng)目啟動(dòng)的時(shí)候,運(yùn)行main方法報(bào)錯(cuò)NoClassDefFoundError問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01關(guān)于IDEA 2020使用 mybatis-log-plugin插件的問題
這篇文章主要介紹了關(guān)于IDEA 2020使用 mybatis-log-plugin插件的問題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-11-11SpringMVC使用MultipartFile實(shí)現(xiàn)文件上傳
這篇文章主要為大家詳細(xì)介紹了SpringMVC使用MultipartFile實(shí)現(xiàn)文件上傳功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04RocketMQ實(shí)現(xiàn)隨緣分BUG小功能示例詳解
這篇文章主要為大家介紹了RocketMQ實(shí)現(xiàn)隨緣分BUG小功能示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08MyBatis?多表聯(lián)合查詢及優(yōu)化方法
大家都知道Hibernate 是全自動(dòng)的數(shù)據(jù)庫持久層框架,它可以通過實(shí)體來映射數(shù)據(jù)庫,通過設(shè)置一對(duì)多、多對(duì)一、一對(duì)一、多對(duì)多的關(guān)聯(lián)來實(shí)現(xiàn)聯(lián)合查詢,接下來通過本文給大家介紹MyBatis?多表聯(lián)合查詢及優(yōu)化,需要的朋友可以參考下2022-08-08SpringBoot如何優(yōu)雅的處理重復(fù)請(qǐng)求
對(duì)于一些用戶請(qǐng)求,在某些情況下是可能重復(fù)發(fā)送的,如果是查詢類操作并無大礙,但其中有些是涉及寫入操作的,一旦重復(fù)了,可能會(huì)導(dǎo)致很嚴(yán)重的后果,所以本文給大家介紹了SpringBoot優(yōu)雅的處理重復(fù)請(qǐng)求的方法,需要的朋友可以參考下2023-12-12Spring實(shí)戰(zhàn)之搜索Bean類操作示例
這篇文章主要介紹了Spring實(shí)戰(zhàn)之搜索Bean類操作,結(jié)合實(shí)例形式分析了Spring搜索Bean類的相關(guān)配置、接口實(shí)現(xiàn)與操作技巧,需要的朋友可以參考下2019-12-12SpringBoot如何使用TestEntityManager進(jìn)行JPA集成測(cè)試
TestEntityManager是Spring Framework提供的一個(gè)測(cè)試框架,它可以幫助我們進(jìn)行 JPA 集成測(cè)試,在本文中,我們將介紹如何使用 TestEntityManager 進(jìn)行 JPA 集成測(cè)試,感興趣的跟著小編一起來學(xué)習(xí)吧2023-06-06