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:
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.
java spring spring-transactions jpa transactionmanager
Brian DiCasa Dec 12 '10 at 18:10 2010-12-12 18:10
source share