0
在我的項目中,我需要在一個事務中處理更多的數據庫。交易註釋或xml
1:使用註釋,此報告錯誤 「重複標註」
public class TransactionalService {
@Transactional("order")
@Transactional("account")
public void processTwoDatabases(String name) { ... }
}
XML段跟隨
<bean id="transactionManager1"
class="org.springframework.jdbc.DataSourceTransactionManager">
<qualifier value="order"/>
</bean>
<bean id="transactionManager2"
class="org.springframework.jdbc.DataSourceTransactionManager">
<qualifier value="account"/>
</bean>
2:但是,使用XML,它工作正常:
<tx:advice id="txAdvice1" transaction-manager="transactionManager1">
<!-- 定義方法的過濾規則 -->
<tx:attributes>
<tx:method name="process*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution (* com.service.impl.*.*(..))" id="services1"/>
<aop:advisor advice-ref="txAdvice1" pointcut-ref="services1"/>
</aop:config>
<tx:advice id="txAdvice2" transaction-manager="transactionManager2">
<tx:attributes>
<tx:method name="process*" propagation="REQUIRED" read-only="false"
rollback-for="java.lang.Exception"/>
</tx:attributes>
</tx:advice>
<aop:config proxy-target-class="true">
<aop:pointcut expression="execution (* com.service.impl.*.*(..))" id="services2"/>
<aop:advisor advice-ref="txAdvice2" pointcut-ref="services2"/>
</aop:config>
我要回滾所有交易,如果這兩個交易中的任何一個失敗。在我的方式,兩個transations如下:
'(1)爲Transaction1開始' '(2)transaction2開始' '(3)我的業務邏輯代碼' '(4)交易2端' '(5 )transaction1結束' 所以在1-4之間引發任何異常,兩個事務將回滾。唯一的飛行是(4)和(5)之間拋出的異常,只有外部事務將回滾 ,但內部事務將提交.'@Roman Puchkovskiy' –
請注意我關於XT Transaction的編輯 –