title: JPA Config with two database
body: <p>I have a application pointing to two different database, I have created two JPAConfig following the example posted here (The JPA Configuration with JAVA at 2)</p>
  
  <p><a href="http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/#noxml" rel="nofollow">http://www.baeldung.com/2011/12/13/the-persistence-layer-with-spring-3-1-and-jpa/#noxml</a></p>
  
  <p>In my persistence layer I use anotations and code is something like this persistor interface</p>
  
  <pre><code>public interface EmployeePersistor {
  
      public void create(EmployeeEntity employee);
  
  }
  </code></pre>
  
  <p>Implementation is </p>
  
  <pre><code> @Component
      public class EnvelopeStatusPersistorImpl implements EnvelopeStatusPersistor {
  
          @PersistenceContext(unitName = "db2EntityManagerFactory")
          EntityManager db2EM;
  
          @Override
          @Transactional("db2TransactionManager")
          public void create(EmployeeEntity employee) {
  
              db2EM.persist(employee);
              db2EM.flush();
          }
  
  }
  </code></pre>
  
  <p>This works fine for db2 transactions , but when I try to do similar approach with db1 , the query are still running against db2</p>
  
  <pre><code>@PersistenceContext(unitName = "db1EntityManagerFactory")
          EntityManager db1EM;
  
          @Override
          @Transactional("db1TransactionManager")
          public void create(EmployeeEntity employee) {
  
              db1EM.persist(employee);
              db1EM.flush();
          }
  </code></pre>
  
  <p>Listed below is the JPAConfig file for just db1</p>
  
  <pre><code>@Configuration
  @PropertySource("classpath:application.${target_env}.properties")
  @EnableTransactionManagement
  @EnableJpaRepositories(basePackages = "com.application", entityManagerFactoryRef = "db1EntityManagerFactory", transactionManagerRef = "db1TransactionManager")
  public class CRMJPAConfig {
  
      @Value("${db1.url}")
      private String db1URL;
  
      @Value("${db1.username}")
      private String db1User;
  
      @Value("${db1.password}")
      private String db1Password;
  
      @Value("${db1.driver}")
      private String db1DriverClass;
  
      @Value("${db1.scan}")
      private String db1PackageScan;
  
      @Value("${db1.dialect}")
      private String db1HibernateDialect;
  
      @Bean
      @Primary
      public LocalContainerEntityManagerFactoryBean db1EntityManagerFactory() {
          LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
          em.setDataSource(dataSource());
          em.setPackagesToScan(new String[] { db1PackageScan });
  
          JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
          em.setJpaVendorAdapter(vendorAdapter);
          em.setJpaProperties(additionalProperties());
  
          return em;
      }
  
      @Bean
      @Primary
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName(db1DriverClass);
          dataSource.setUrl(db1URL);
          dataSource.setUsername(db1User);
          dataSource.setPassword(db1Password);
          return dataSource;
      }
  
      @Bean
      @Primary
      public PlatformTransactionManager db1TransactionManager() {
          JpaTransactionManager transactionManager = new JpaTransactionManager();
          transactionManager.setEntityManagerFactory(db1EntityManagerFactory()
                  .getObject());
  
          return transactionManager;
      }
  
      @Bean
      public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
          return new PersistenceExceptionTranslationPostProcessor();
      }
  
      Properties additionalProperties() {
          Properties properties = new Properties();
          properties.setProperty("hibernate.hbm2ddl.auto", "none");
          properties.setProperty("hibernate.dialect", db1HibernateDialect);
          return properties;
      }
  }
  </code></pre>