有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring引导jpa数据使用错误的数据库

我有一个spring boot应用程序,它拉入一个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而不是从mysqlTransactionManager获取数据

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

@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 boot应用程序中,我就可以连接到正确的数据源,没有问题。但当我把它们搬到外面的图书馆时,它就不起作用了。由于我的项目的结构,我必须将它们保存在一个公共库中,两个单独的应用程序使用此数据库配置

如果必要的话,我可以自由地将非主db移动到我的spring boot应用程序中,但这也不起作用。结果是一样的


共 (0) 个答案