【问题标题】:Spring batch boot Multiple datasources Multiple schemasSpring批量启动多个数据源多个模式
【发布时间】:2018-09-01 06:36:45
【问题描述】:

我有一个使用 spring boot 的 spring 批处理作业,它有 2 个数据源。每个数据源又有 2 个模式。我需要为两个数据源指定默认架构。我知道我用来为一个数据源指定默认模式的属性 spring.jpa.properties.hibernate.default_schema。有没有办法为另一个模式指定默认模式?

目前,要为其他数据源指定默认架构,我正在使用更改会话查询来根据需要切换架构。我正在尝试从我的 java 代码中删除这个 alter session 查询。非常感谢您对此提出任何建议。

编辑 1:两者都是 ORACLE 数据库

【问题讨论】:

  • 如果数据库是 postgres,那么您可以在 jdbc url 中定义它,如下所示 jdbc:postgresql://localhost:5432/mydatabase?currentSchema=myschema
  • 两个数据库都是 oracle :(
  • 您可以创建 4 个数据源,每个数据库/模式组合一个?

标签: spring-boot spring-batch


【解决方案1】:

如果您使用多个数据源,那么您可能对每个数据源都有一个@Configuration 类。在这种情况下,您可以为entityManager 设置其他属性。需要这个配置:

props.put("spring.datasource.schema", "test");

完整示例

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "testEntityManagerFactory", transactionManagerRef = "testTransactionManager",
    basePackages = {"com.test.repository"})
public class TestDbConfig {

  @Bean(name = "testDataSource")
  @ConfigurationProperties(prefix = "test.datasource")
  public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
  }

  @Bean(name = "testEntityManagerFactory")
  public LocalContainerEntityManagerFactoryBean entityManagerFactory(EntityManagerFactoryBuilder builder, @Qualifier("testDataSource") DataSource dataSource) {
    return builder.dataSource(dataSource).packages("com.test.model").persistenceUnit("test").properties(jpaProperties()).build();
  }

  private Map<String, Object> jpaProperties() {
    Map<String, Object> props = new HashMap<>();
    props.put("hibernate.physical_naming_strategy", SpringPhysicalNamingStrategy.class.getName());
    props.put("hibernate.implicit_naming_strategy", SpringImplicitNamingStrategy.class.getName());
    props.put("spring.datasource.schema", "test");
    return props;
  }

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

【讨论】:

    猜你喜欢
    • 2017-07-19
    • 1970-01-01
    • 2018-12-11
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多