我在不同的類中有方法 A 和方法 B。兩者都用@Transactional 注釋。方法A正在呼叫B。有沒有辦法在不同的事務中呼叫B而不掛起A事務?B 上的 Propagation.REQUIRES_NEW 提供了在呼叫時始終創建新事務但掛起呼叫者事務的可能性
uj5u.com熱心網友回復:
據我所知,你不能用注釋 REQUIRED_NEW 來做到這一點。
但是 spring 也支持使用 TransactionTemplate、TransactionDefinition 手動事務。
我們可以通過一個 PlatformTransactionManager 使用多個定義。
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/support/TransactionTemplate.html
https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/transaction/TransactionDefinition.html
https://www.baeldung.com/spring-programmatic-transaction-management#4-custom-transaction-configurations
uj5u.com熱心網友回復:
事務傳播行為Propagation.NESTED支持該需求。
請通讀 Spring 官方檔案:Understanding PROPAGATION_NESTED
PROPAGATION_NESTED 使用具有多個可以回滾到的保存點的單個物理事務。這種部分回滾讓內部事務作用域觸發其作用域的回滾,盡管某些操作已回滾,但外部事務仍能夠繼續物理事務。
也可以在此答案中閱讀有關 Spring 事務中嵌套傳播的其他詳細資訊。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/391917.html
標籤:春天 spring-aop 弹簧注解
