天天看點

spring多事務管理

spring多事務管理

此處連結是在stackoverflow上的提出的問題

applicationContext.xml配置檔案的一部分

<bean id="transactionManager1" class="org.springframework.orm.jpa.JpaTransactionManager">   
  <property name="entityManagerFactory" ref="entityManagerFactory1" />   
  <qualifier value="account"/>   
</bean>  
  
<bean id="transactionManager2" class="org.springframework.orm.jpa.JpaTransactionManager">  
  <property name="entityManagerFactory" ref="entityManagerFactory2" />   
  <qualifier value="businessData"/>   
</bean>
           

在代碼中使用方式如下:

public class TransactionalService {  
  
    @Transactional("account")  
    public void setSomethingInAccount() { ... }  
  
    @Transactional("businessData")  
    public void doSomethingInBusinessData() { ... }  
}  
           

通過這種方式,在Spring應用中可以很簡單的通過事務管理使用多個事務連接配接。

原文位址:http://gik.firetrot.com/index.php/2013/12/06/spring-with-multiple-transaction-managers/