title: Spring Boot, Spring Data JPA with multiple DataSources
body: <p>I'm trying to connect each @Repositories to different DataSource(s) with Spring Boot and Spring Data JPA. I used the following, <a href="http://xantorohara.blogspot.com/2013/11/spring-boot-jdbc-with-multiple.html" rel="noreferrer">http://xantorohara.blogspot.com/2013/11/spring-boot-jdbc-with-multiple.html</a>, as a referrence.  Here is the code I am using in an attempt to implement a similar solution using Spring Data JPA.</p>
  
  <p><strong>CustomerDbConfig.java</strong>(First data source connection)</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(
          entityManagerFactoryRef = "orderEntityManager",
          transactionManagerRef = "orderTransactionManager",
          basePackages = {"com.mm.repository.customer"})
  public class CustomerDbConfig {
  
      @Bean(name = "customerEntityManager")
      public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(dataSource());
          em.setPackagesToScan(new String[] {"com.mm.domain.customer"});
  
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          em.setJpaProperties(additionalJpaProperties());
          em.setPersistenceUnitName("customerPersistence");
          em.setPackagesToScan("com.mm.domain.customer");
  
          return em;
      }
  
      Properties additionalJpaProperties(){
          Properties properties = new Properties();
          properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
          properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
          properties.setProperty("hibernate.show_sql", "true");
  
          return properties;
      }
  
      @Bean
      public DataSource dataSource(){
          return DataSourceBuilder.create()
                  .url("jdbc:h2:mem:customer:H2")
                  .driverClassName("org.h2.Driver")
                  .username("sa")
                  .password("")
                  .build();
      }   
  
      @Bean(name = "customerTransactionManager")
      public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(emf);
  
          return transactionManager;
      }
  }
  </code></pre>
  
  <p><strong>CustomerDbConfig.java</strong> (Second data source)</p>
  
  <pre><code>@Configuration
  @EnableJpaRepositories(
          entityManagerFactoryRef = "orderEntityManager",
          transactionManagerRef = "orderTransactionManager",
          basePackages = {"com.mm.repository.customer"})
  public class CustomerDbConfig {
  
      @Bean(name = "customerEntityManager")
      public LocalContainerEntityManagerFactoryBean entityManagerFactory(){
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(dataSource());
          em.setPackagesToScan(new String[] {"com.mm.domain.customer"});
  
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          em.setJpaProperties(additionalJpaProperties());
          em.setPersistenceUnitName("customerPersistence");
          em.setPackagesToScan("com.mm.domain.customer");
  
          return em;
      }
  
      Properties additionalJpaProperties(){
          Properties properties = new Properties();
          properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
          properties.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
          properties.setProperty("hibernate.show_sql", "true");
  
          return properties;
      }
  
      @Bean
      public DataSource dataSource(){
          return DataSourceBuilder.create()
                  .url("jdbc:h2:mem:customer:H2")
                  .driverClassName("org.h2.Driver")
                  .username("sa")
                  .password("")
                  .build();
      }   
  
      @Bean(name = "customerTransactionManager")
      public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(emf);
  
          return transactionManager;
      }
  }
  </code></pre>
  
  <p><strong>Customer.java</strong> (model)</p>
  
  <pre><code>@Entity
  @Table(name = "customer")
  @Data
  @EqualsAndHashCode(exclude = {"id"})
  public class Customer {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer id;
  
      @Column(name = "name", nullable = false)
      private String name;
  
      @Column(name = "age", nullable = false)
      private Integer age;
  
  ....
  </code></pre>
  
  <p><strong>Order.java</strong> (model)</p>
  
  <pre><code>@Entity
  @Table(name = "order")
  @Data
  @EqualsAndHashCode(exclude = {"id"})
  public class Order {
  
      @Id
      @GeneratedValue(strategy = GenerationType.AUTO)
      private Integer id;
  
      @Column(name = "code", nullable = false)
      private Integer code;
  
      @Column(name = "quality", nullable = false)
      private Integer quality;
  </code></pre>
  
  <p>...</p>
  
  <p><strong>CustomerRepository.java</strong></p>
  
  <pre><code>public interface CustomerRepository extends JpaRepository&lt;Customer, Integer&gt;{
  
  }
  </code></pre>
  
  <p><strong>OrderRepository.java</strong></p>
  
  <pre><code>public interface OrderRepository extends JpaRepository&lt;Order, Integer&gt; {
  
  }
  </code></pre>
  
  <p>Finally, <strong>Application.java</strong></p>
  
  <pre><code>@Configuration
  @ComponentScan
  @EnableAutoConfiguration
  public class Application extends SpringApplication{
  
         public static void main(String[] args) {
              SpringApplication.run(Application.class, args);
          }
  
          @Bean
          public ServletRegistrationBean h2Console() {
              ServletRegistrationBean reg = new ServletRegistrationBean(new WebServlet(), "/console/*");
              reg.setLoadOnStartup(1);
              return reg;
          }
  }
  </code></pre>
  
  <p>During start the following <strong>exceptions</strong> are thrown:</p>
  
  <pre><code>-10-10 15:45:24.757 ERROR 1549 --- [           main] o.s.boot.SpringApplication               : Application startup failed
  
  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerTransactionManager' defined in class path resource [com/mm/boot/multidb/CustomerConfig.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [javax.persistence.EntityManagerFactory]: : No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: customerEntityManager,orderEntityManager; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: customerEntityManager,orderEntityManager
      at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:747)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:462)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1095)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:990)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
      at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
      at com.mm.boot.multidb.Application.main(Application.java:17)
  Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: customerEntityManager,orderEntityManager
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:974)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
      at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:811)
      at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:739)
      ... 18 common frames omitted
  
  Exception in thread "main" org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'customerTransactionManager' defined in class path resource [com/mm/boot/multidb/CustomerConfig.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [javax.persistence.EntityManagerFactory]: : No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: customerEntityManager,orderEntityManager; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: customerEntityManager,orderEntityManager
      at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:747)
      at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:462)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1095)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:990)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
      at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
      at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302)
      at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
      at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
      at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:706)
      at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
      at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
      at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
      at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:952)
      at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
      at com.mm.boot.multidb.Application.main(Application.java:17)
  Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined: expected single matching bean but found 2: customerEntityManager,orderEntityManager
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:974)
      at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:862)
      at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:811)
      at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:739)
      ... 18 more
  </code></pre>
  
  <p>Full code for the sample can be found on GitHub (<a href="https://github.com/tonym2105/samples/tree/master/boot-multidb-sample" rel="noreferrer">https://github.com/tonym2105/samples/tree/master/boot-multidb-sample</a>)</p>
  
  <p>Thank you in advance for the help.</p>