Take the 2-minute tour ×

I'm new spring and I'm wondering if its possible to use numerous transaction managers in the same application?

I have two data access layers - one for both of the databases. I'm wondering, how do you go about using one transaction managers for one layer and different transaction manager for the other layer. I don't need to perform transactions across both databases - yet. But I do need perform transactions on each database individually. I've created an image to help outline my problem:

alt text

Here is my application context configuration:


    <context:component-scan base-package="cheetah.repositories" />
    <tx:annotation-driven />

    <bean id="entityManagerFactory"
        <property name="persistenceUnitName" value="accounts" />

    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

    <bean id="transactionManager"


Here is an example that uses this configuration:

public class JpaAccountRepository implements AccountRepository {

    @PersistenceContext(unitName = "cheetahAccounts")
    private EntityManager accountManager;

    public Account findById(long id) {

        Account account = accountManager.find(Account.class, id);
        return account;

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

Thanks for any help.

share|improve this question
add comment

2 Answers

up vote 24 down vote accepted

Where you use a @Transactional annotation, you can specify the transaction manager to use by adding an attribute set to a bean name or qualifier. For example, if your application context defines multiple transaction managers with qualifiers:

<bean id="transactionManager1"
    <property name="entityManagerFactory" ref="entityManagerFactory1" />
    <qualifier value="account"/>

<bean id="transactionManager2"
    <property name="entityManagerFactory" ref="entityManagerFactory2" />
    <qualifier value="businessData"/>

You can use the qualifier to specify the transaction manager to use:

public class TransactionalService {

    public void setSomethingInAccount() { ... }

    public void doSomethingInBusinessData() { ... }
share|improve this answer
Nice one - good answer . :-) –  David Victor Nov 3 '11 at 16:34
What about if there is a method that reads data from the database where you dont need to use the @Transactional annotation for database reads. –  ziggy May 24 '12 at 11:58
add comment

This Spring Jira entry discusses the issue a bit:


I think it could be one transaction manager per connection if you're not using two-phase commit. You just need to create two transaction managers and inject them with the appropriate connection.

But I must ask the question: why do you think you need two transaction managers? You can have more than one database connection. The DAOs that use the connections can and should be instantiated by different services, each of which can be annotated with their own transactional settings. One manager can accommodate both. Why do you think you need two?

share|improve this answer
I thought I needed two transaction managers because the transaction manager specifies the entity manager factory, which in turn specifies the persistence unit to be used. –  Brian DiCasa Dec 12 '10 at 18:26
@Brian that sounds reasonable @Duffymo +1 –  Sean Patrick Floyd Dec 12 '10 at 21:02
add comment

Your Answer


