title: Two entity managers in Spring
body: <p>I had done this programmatically at some point - that is creating two entity managers with different data sources and providing them for different services.</p>
  
  <p>However now I am building a webApp using Spring. Thing is that I want to have two entity managers responsible for separate databases. So in my case, half of DAO's would be using <strong>emNumber1</strong>, and second half would be using <strong>emNumber2</strong>. Oh and totally different persistence units as I only need to read data from <strong>DB1</strong> and then process it and store this data in <strong>DB2</strong> in totally different entities.</p>
  
  <p>I am well aware that this problem is and has been going around for sometime now but in different shapes and forms and so I am composing this question only because I could not understand the solutions that were presented withing those googled-up forum threads or they were not applicable to my case. In same cases I could not understand the solution or how to apply it in my case so really sorry for redundant question if it were to be solved by linking a solution from this very own site.</p>
  
  <p>Here is how I find fit to configure a single transaction manager</p>
  
  <pre><code>&lt;tx:annotation-driven transaction-manager="transactionManager" /&gt;
  &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/&gt;
  
  &lt;bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource"
        p:driverClassName="com.mysql.jdbc.Driver"
        p:url="jdbc:mysql://localhost:3306/db"
        p:username="dbuser"
        p:password="dbuser"/&gt;
  
  &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
      &lt;property name="persistenceUnitName" value="pu1"/&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
      &lt;property name="jpaVendorAdapter"&gt;
          &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt;
              &lt;property name="showSql" value="true"/&gt;
              &lt;property name="generateDdl" value="true"/&gt;
              &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/&gt;
          &lt;/bean&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  
  &lt;bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"&gt;
      &lt;property name="entityManagerFactory" ref="entityManagerFactory" /&gt;
      &lt;property name="dataSource" ref="dataSource"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>Then inside my dao:</p>
  
  <pre><code>public class SimpleDaoImpl implements SimpleDao {
  @PersistenceContext
  private EntityManager entityManager;
  
      /* methods that use entity manager for transactions and data detcing */
  }
  </code></pre>
  
  <p>So in case of two transaction managers with different data sources how would one specify what entity manager to inject into which DAO or is that impossible in Spring?</p>