Spring - Can I use multiple transaction managers in one application?

I'm new to Spring, and I wonder if multiple transaction managers can be used in one application?

I have two levels of data access - one for both databases. I am wondering how you are going to use one transaction manager for one level and another transaction manager for another level. I do not need to perform transactions on both databases. But I need to perform transactions on each database individually. I created an image to help describe my problem:

alt text

Here is my application context configuration:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <context:component-scan base-package="cheetah.repositories" /> <tx:annotation-driven /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="accounts" /> </bean> <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> </beans> 

Here is an example that uses this configuration:

 @Repository public class JpaAccountRepository implements AccountRepository { @PersistenceContext(unitName = "cheetahAccounts") private EntityManager accountManager; @Override @Transactional public Account findById(long id) { Account account = accountManager.find(Account.class, id); return account; } } 

So, for the account repository, I want to use the factory entity manager with the storage unit set for the accounts. However, with my BusinessData repository, I want to use the factory entity manager with a different save unit. Since I can only define one bean transaction manager, how can I use different transaction managers for different repositories?

Thanks for any help.

+54
java spring spring-transactions jpa transactionmanager
Dec 12 '10 at 18:10
source share
2 answers

If you use the @Transactional annotation, you can specify the transaction manager to use by adding the attribute set to the bean name or classifier. For example, if your application context defines several transaction managers with qualifiers:

 <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> 

You can use the qualifier to specify the transaction manager:

 public class TransactionalService { @Transactional("account") public void setSomethingInAccount() { ... } @Transactional("businessData") public void doSomethingInBusinessData() { ... } } 
+75
Dec 12 2018-10-12
source share

This Spring Jira post discusses the issue a bit:

https://jira.spring.io/browse/SPR-3955

I think this could be one transaction manager for each connection if you are not using two-phase commit. You just need to create two transaction managers and enter them with the appropriate connection.

But I have to ask a question: why do you think you need two transaction managers? You may have several database connections. DAOs that use connections can and should be created by various services, each of which can be annotated with their own transaction settings. One manager can host both. Why do you think you need two?

+5
Dec 12 '10 at 18:15
source share



All Articles