title: Spring - Is it possible to use multiple transaction managers in the same application?
body: <p>I'm new to Spring and I'm wondering if its possible to use numerous transaction managers in the same application?</p>
  
  <p>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:</p>
  
  <p><img src="https://i.stack.imgur.com/zt4PX.png" alt="alt text"></p>
  
  <p>Here is my application context configuration:</p>
  
  <pre><code>&lt;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"&gt;
  
      &lt;context:component-scan base-package="cheetah.repositories" /&gt;
      &lt;tx:annotation-driven /&gt;
  
      &lt;bean id="entityManagerFactory"
          class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"&gt;
          &lt;property name="persistenceUnitName" value="accounts" /&gt;
      &lt;/bean&gt;
  
      &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" /&gt;
  
      &lt;bean id="transactionManager"
          class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
          &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Here is an example that uses this configuration:</p>
  
  <pre><code>@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;
      }
  }
  </code></pre>
  
  <p>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?</p>
  
  <p>Thanks for any help.</p>