Spring使用错误的数据库启动jpa数据

时间:2017-07-19 17:36:10

标签: java spring spring-data spring-data-jpa

我有一个弹簧启动应用程序,它拉入一个有两个数据库配置的jar,两个都是mysql db。两个数据库看起来都在日志中正确启动,但不是唯一标识的。看起来默认已注册,然后重新注册,因此它似乎会覆盖。

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=] 

我有两个用于配置数据库连接的类,这两个类都位于导入的jar中,基本上是一个公共库。

@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
...
}

 @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 {
    ...
    }

我尝试通过这样的类从每个数据库中获取数据:

@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 {
    }

PriceService获取数据很好,但PriceService2尝试从mysqlTransactionManager而不是mysqlTransactionManager2获取数据。

我还有以下类来运行我的启动应用程序:

@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);
    }

}

如何强制一个类使用特定的事务管理器/数据源?我一直在尝试各种配置,据我所知,这是正确设置的。如果我将这两个数据源配置类放入我的spring启动应用程序中,我可以连接到正确的数据源没问题。但是,当我将它们移动到外部库时,它不起作用。由于我的项目结构,我必须将它们保存在一个公共库中,两个独立的应用程序使用此数据库配置。

如果有必要,我可以自由地将非主数据库移动到我的春季启动应用程序中,但这也无效。结果相同。

0 个答案:

没有答案