如何在Sping Boot 中配置两个Oracle数据源?

pxiryf3j  于 2023-05-16  发布在  Oracle
关注(0)|答案(1)|浏览(162)

在spring Boot jpa app中尝试配置两个oracle数据源(PCP&PP)时遇到了问题。
有人能告诉我我的配置有什么问题吗?

PCPDatabaseConfig required a bean of type 'org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder' that could not be found.

应用程序的结构和配置如下:

application.yml配置:

spring:
  application:
    name: myapp
    group: pp
    product: YYY
    area: delivery
    host: ${HOST:localhost}
  profiles:
    active: ${ENV}
  jmx.enabled: false
  datasource:
    pp:
      platform: oracle
      connection-init-sql: call DBMS_APPLICATION_INFO.SET_MODULE ('${spring.application.name}', null)
      jmx.enabled: false
      maximum-pool-size: 5
      driver-class-name: oracle.jdbc.OracleDriver
    pcp:
      platform: oracle
      connection-init-sql: call DBMS_APPLICATION_INFO.SET_MODULE ('${spring.application.name}', null)
      jmx.enabled: false
      maximum-pool-size: 5
      driver-class-name: oracle.jdbc.OracleDriver

结构:

PCPDatabaseConfig:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.xxx.repository.pcp")
public class PCPDatabaseConfig {
    @Primary
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.pcp")
    public DataSource pcpDataSource() {
        return DataSourceBuilder.create()
                .build();
    }
    @Primary
    @Bean
    public JdbcTemplate pcpJdbcTemplate(@Qualifier("pcpDataSource") DataSource pcpDataSource) {
        return new JdbcTemplate(pcpDataSource);
    }

    @ConfigurationProperties(prefix = "spring.jpa")
    public JpaVendorAdapter jpaVendorAdapterOraclePCP() {
        return new HibernateJpaVendorAdapter();
    }
    @Primary
    @Bean(name = "entityManagerFactoryPCP")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPCP(EntityManagerFactoryBuilder builder) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = builder
                .dataSource(pcpDataSource())
                .packages("com.xxx.repository.pcp")
                .persistenceUnit("oracle")
                .build();
        entityManagerFactory.setJpaVendorAdapter(jpaVendorAdapterOraclePCP());
        return entityManagerFactory;
    }
    @Primary
    @Bean(name = "oraclesqlTransactionManagerPCP")
    public PlatformTransactionManager oraclesqlTransactionManagerPCP(@Qualifier("entityManagerFactoryPCP") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

}

PPDatabaseConfig:

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(basePackages = "com.xxx.repository.pp")
public class PPDatabaseConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.pp")
    public DataSource ppDataSource() {
        return DataSourceBuilder.create()
                .build();
    }
    @Bean
    public JdbcTemplate ppJdbcTemplate(@Qualifier("ppDataSource") DataSource ppDataSource) {
        return new JdbcTemplate(ppDataSource);
    }

    @Bean(name = "entityManagerFactoryPP")
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryPP(
            EntityManagerFactoryBuilder builder
    ) {
        LocalContainerEntityManagerFactoryBean entityManagerFactory = builder
                .dataSource(ppDataSource())
                .packages("com.xxx.repository.pp")
                .persistenceUnit("oracle")
                .build();
        return entityManagerFactory;
    }
    @Bean(name = "oraclesqlTransactionManagerPP")
    public PlatformTransactionManager oraclesqlTransactionManagerPP(@Qualifier("entityManagerFactoryPP") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }
}
vnzz0bqm

vnzz0bqm1#

我修复了问题如下(发现问题后也更新了问题):
1.使用@EnableJpaRepositories,如:

@EnableJpaRepositories( entityManagerFactoryRef = "entityManagerFactoryPCP",
        basePackages = "com.xxx.repository.pcp")
public class PCPDatabaseConfig {

然后呢

@EnableJpaRepositories(entityManagerFactoryRef = "entityManagerFactoryPP",
            basePackages = "com.xxx.repository.pp")
public class PPDatabaseConfig {

然后呢
使用精确的@Bean(name="transactionManager"),如:

@Bean(name="transactionManager")
    public PlatformTransactionManager oraclesqlTransactionManagerPCP(@Qualifier("entityManagerFactoryPCP") EntityManagerFactory entityManagerFactory) {
        return new JpaTransactionManager(entityManagerFactory);
    }

相关问题