title: Spring boot jpa data using wrong database
body: <p>I have a spring boot app, it pulls in a jar which has two database configs, both mysql db's.  Both databases look to be starting up correctly in the logs, but are not uniquely identified.  It looks like default is registered and then reregistered so it appears to overwrite.</p>
  
  <pre><code>2017-07-19 10:24:16,817 INFO  DriverManagerDataSource - Loaded JDBC driver: com.mysql.jdbc.Driver [tx-id=] 
  2017-07-19 10:24:16,937 INFO  LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default' [tx-id=] 
  2017-07-19 10:24:16,947 INFO  LogHelper - HHH000204: Processing PersistenceUnitInfo [
      name: default
      ...] [tx-id=] 
  2017-07-19 10:24:16,991 INFO  Version - HHH000412: Hibernate Core {5.0.12.Final} [tx-id=] 
  2017-07-19 10:24:16,992 INFO  Environment - HHH000206: hibernate.properties not found [tx-id=] 
  2017-07-19 10:24:16,993 INFO  Environment - HHH000021: Bytecode provider name : javassist [tx-id=] 
  2017-07-19 10:24:17,021 INFO  Version - HCANN000001: Hibernate Commons Annotations {5.0.1.Final} [tx-id=] 
  2017-07-19 10:24:17,379 INFO  Dialect - HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect [tx-id=] 
  2017-07-19 10:24:17,593 WARN  RootClass - HHH000038: Composite-id class does not override equals(): com.example.EmployeeGroupEntity$EmployeeGroupId [tx-id=] 
  2017-07-19 10:24:17,593 WARN  RootClass - HHH000039: Composite-id class does not override hashCode(): com.example.EmployeeGroupEntity$EmployeeGroupId [tx-id=] 
  2017-07-19 10:24:17,943 INFO  LocalContainerEntityManagerFactoryBean - Initialized JPA EntityManagerFactory for persistence unit 'default' [tx-id=] 
  2017-07-19 10:24:17,970 INFO  DriverManagerDataSource - Loaded JDBC driver: net.sourceforge.jtds.jdbc.Driver [tx-id=] 
  2017-07-19 10:24:17,972 INFO  LocalContainerEntityManagerFactoryBean - Building JPA container EntityManagerFactory for persistence unit 'default' [tx-id=] 
  2017-07-19 10:24:17,972 INFO  LogHelper - HHH000204: Processing PersistenceUnitInfo [
      name: default
      ...] [tx-id=] 
  2017-07-19 10:24:18,370 INFO  Dialect - HHH000400: Using dialect: org.hibernate.dialect.SQLServerDialect [tx-id=] 
  2017-07-19 10:24:18,389 INFO  LobCreatorBuilderImpl - HHH000423: Disabling contextual LOB creation as JDBC driver reported JDBC version [3] less than 4 [tx-id=] 
  2017-07-19 10:24:18,394 WARN  RootClass - HHH000038: Composite-id class does not override equals(): com.example.EmployeeGroupEntity$EmployeeGroupId [tx-id=] 
  2017-07-19 10:24:18,394 WARN  RootClass - HHH000039: Composite-id class does not override hashCode(): com.example.entity.EmployeeGroupEntity$EmployeeGroupId [tx-id=] 
  2017-07-19 10:24:18,405 WARN  EntityManagerFactoryRegistry - HHH000436: Entity manager factory name (default) is already registered.  If entity manager will be clustered or passivated, specify a unique value for property 'hibernate.ejb.entitymanager_factory_name' [tx-id=] 
  </code></pre>
  
  <p>I have two classes for configuring the database connections, both of these are located in the imported jar, basically a common library.</p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @PropertySource(value = { "classpath:/${app.execution.environment}/application.properties" })
  @EnableJpaRepositories(basePackages = "com.example", entityManagerFactoryRef = "mysqlEntityManager", transactionManagerRef = "mysqlTransactionManager")
  @EntityScan(basePackages = { "com.example" })
  public class MysqlHibernateConfig {
  // beans configured here marked with @Primary
  ...
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code> @Configuration
      @EnableTransactionManagement
      @PropertySource(value = { "classpath:/${app.execution.environment}/application.properties" })
      @EnableJpaRepositories(basePackages = "com.example.another_package", entityManagerFactoryRef = "mysqlEntityManager2", transactionManagerRef = "mysqlTransactionManager2")
      @EntityScan(basePackages = { "com.example.another_package" })
      public class MysqlHibernateConfig2 {
      ...
      }
  </code></pre>
  
  <p>I try to get data from each database through classes like this:</p>
  
  <pre><code>@Transactional("mysqlTransactionManager") // workspace complains about not being able to find this bean
  @Service
  public class PriceService {
  }
  
  @Transactional("mysqlTransactionManager2") // workspace complains about not being able to find this bean
      @Service
      public class PriceService2 {
      }
  </code></pre>
  
  <p>PriceService gets data just fine, but PriceService2 tries to get data from mysqlTransactionManager instead of from mysqlTransactionManager2.</p>
  
  <p>I also have the following classes to run my boot app:</p>
  
  <pre><code>@Configuration
  @ComponentScan(basePackages = "com.example")
  public class WebMvcConfig extends WebMvcConfigurerAdapter {
  //config stuff
  }
  
  @SpringBootApplication
  public class Application {
  
      public static void main(String[] args) {
          SpringApplication.run(Application.class, args);
      }
  
  }
  </code></pre>
  
  <p>How can I force one class to use a specific transaction manager/data source?  I have been trying various configs and as far as I can tell this is set up correctly.  If I put both of these datasource config classes into my spring boot app, I can connect to the correct datasources no problem.  But when I move them to an outside library it does not work.  I have to keep these in a common library because of the structure of my projects, two separate apps use this database config.</p>
  
  <p>I do have the freedom to move the non primary db into my spring boot app if necessary, but that is not working either. Ends up with the same result.</p>