title: How to setup multiple data sources with Spring and JPA
body: <p>In our application, we want to set multiple data source with Spring and JPA. Hence we have created 2 entityManagerFactory, 2 data source and 2 transaction- manager. </p>
  
  <p><strong>web.xml</strong></p>
  
  <pre><code> &lt;param-value&gt;
      /WEB-INF/a_spring.xml
      /WEB-INF/b_spring.xml
   &lt;/param-value&gt;
  </code></pre>
  
  <p><strong>Persistence.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;persistence version="1.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"&gt;
      &lt;persistence-unit name="db1" transaction-type="RESOURCE_LOCAL"&gt;
          &lt;class&gt;com.rh.domain.RcA&lt;/class&gt;
      &lt;/persistence-unit&gt;
  
        &lt;persistence-unit name="db2" transaction-type="RESOURCE_LOCAL"&gt;
        &lt;class&gt;com.rh.domain.Rcb&lt;/class&gt;
      &lt;/persistence-unit&gt;
  &lt;/persistence&gt;
  </code></pre>
  
  <p><strong>a_spring.xml</strong> </p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
      &lt;beans xmlns="http://www.springframework.org/schema/beans"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:tx="http://www.springframework.org/schema/tx" 
             xmlns:aop="http://www.springframework.org/schema/aop"
             xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans.xsd
             http://www.springframework.org/schema/tx
             http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
             http://www.springframework.org/schema/aop
             http://www.springframework.org/schema/aop/spring-aop-2.0.xsd"&gt;
  
        &lt;bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/&gt;  
        &lt;bean id = "RcMaintenanceService" class="com.rh.services.RcAbcMaintenanceServiceImpl" autowire="byName" /&gt;
  
      &lt;aop:config&gt;
              &lt;aop:pointcut id="rOperation" expression="execution(* com.rh.services.*.*(..))"/&gt;
              &lt;aop:advisor advice-ref="txAdvice" pointcut-ref="rOperation"/&gt;
          &lt;/aop:config&gt;
  
      &lt;tx:advice id="txAdvice" transaction-manager="transactionManager"&gt;
              &lt;tx:attributes&gt;
                     &lt;tx:method name="*"/&gt;
              &lt;/tx:attributes&gt;
          &lt;/tx:advice&gt;
  
      &lt;bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"&gt;
              &lt;property name="jndiName" value="java:comp/env/jdbc/db1" /&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;
  
          &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt;
                 &lt;property name="persistenceUnitName" value="db1" /&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="false"/&gt;
                      &lt;property name="database" value="MYSQL" /&gt;
                      &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect"/&gt;
                  &lt;/bean&gt;
              &lt;/property&gt;
              &lt;property name="jpaDialect"&gt;
                  &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"&gt;
                  &lt;/bean&gt;
              &lt;/property&gt;
          &lt;/bean&gt;
  </code></pre>
  
  <p>I also declare another entityManagetFactory,Transaction Manager and dataSource to <strong>b_spring.xml</strong>.</p>
  
  <p><strong>Error</strong></p>
  
  <blockquote>
    <p>Initialization of bean failed; nested exception is
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No
    unique bean of type [javax.persistence.EntityManagerFactory] is
    defined: expected single bean but found 2 Caused by:
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No
    unique bean of type [javax.persistence.EntityManagerFactory] is
    defined: expected single bean but found 2     at
    org.springframework.beans.factory.BeanFactoryUtils.beanOfTypeIncludingAncestors(BeanFactoryUtils.java:303)
      at
    org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findDefaultEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:451)
      at
    org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor.findEntityManagerFactory(PersistenceAnnotationBeanPostProcessor.java:428)
      at
    org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$AnnotatedMember.resolveEntityManager(PersistenceAnnotationBeanPostProcessor.java:582)
      at
    org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$AnnotatedMember.resolve(PersistenceAnnotationBeanPostProcessor.java:553)
      at
    org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor$AnnotatedMember.inject(PersistenceAnnotationBeanPostProcessor.java:489)</p>
  </blockquote>