【问题标题】:Spring batch tables in a different schema不同模式中的 Spring 批处理表
【发布时间】:2018-04-12 20:52:27
【问题描述】:

我想使用不同的模式来保存 Spring Batch 表。我可以在JobRepositoryFactoryBean 中看到我的新数据源。但是这些表仍然是在我有业务表的其他 shcema 中创建的。我在某些地方读到我可以使用dataSource.setValidationQuery 来更改架构,但仍然无法正常工作。我可以解决这个问题。下面是JobRepositoryFactoryBeanDatasource 属性。

 @Bean
 @Qualifier("batchDataSource")
 protected JobRepository createJobRepository() throws Exception {
    JobRepositoryFactoryBean factory = createJobRepositoryFactoryBean();    
    factory.setDataSource(getDataSource());
    if (getDbType() != null) {
      factory.setDatabaseType(getDbType());
    }
    factory.setTransactionManager(getTransactionManager());
    factory.setIsolationLevelForCreate(getIsolationLevel());
    factory.setMaxVarCharLength(maxVarCharLength);
    factory.setTablePrefix(getTablePrefix());
    factory.setValidateTransactionState(validateTransactionState);
    factory.afterPropertiesSet();
    return factory.getObject();
  }

 spring.datasource.url=url
 spring.datasource.username=username
 spring.datasource.password=pwd
spring.datasource.driver-class-name:oracle.jdbc.driver.OracleDriver
spring.datasource.validation-query=ALTER SESSION SET 
 CURRENT_SCHEMA=schemaname

#batch setting
spring.batch.datasource.url=burl
spring.batch.datasource.username=busername
spring.batch.datasource.password=bpwd
spring.batch.datasource.driver-class-name:oracle.jdbc.driver.OracleDriver
spring.batch.datasource.validation-query=ALTER SESSION SET 
CURRENT_SCHEMA=batchschema

 org.apache.tomcat.jdbc.pool.DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
      dataSource.setName("batchDataSourceName");
      dataSource.setDriverClassName(batchDataSourceProperties.getDriverClassName());
      dataSource.setUrl(batchDataSourceProperties.getUrl());
      dataSource.setUsername(batchDataSourceProperties.getUsername());
      dataSource.setPassword(batchDataSourceProperties.getPassword());
     // dataSource.setValidationQuery(batchDataSourceProperties.getValidationQuery());

【问题讨论】:

  • 你在用@EnableBatchProcessing吗?
  • @Michael,是的,我正在使用它
  • 任何人都知道为什么它不保存在不同的架构中
  • 那么您是否使用自定义BatchConfigurer 来确定要用于批处理模式的DataSource
  • @Michael,不。我所做的只是上面代码中给出的内容

标签: schema spring-batch


【解决方案1】:

使用 Spring Batch 的 @EnableBatchProcessing 时,Spring Batch 表使用的 DataSourceBatchConfigurer 提供的那个。如果您在应用程序中使用多个DataSource,您必须创建自己的BatchConfigurer(通过扩展DefaultBatchConfigurer 或实现接口)以便Spring Batch 知道使用哪个。您可以在此处的参考文档中阅读有关此自定义的更多信息:https://docs.spring.io/spring-batch/4.0.x/reference/html/job.html#configuringJobRepository

【讨论】:

  • 好的,谢谢。所以现在我扩展了 DefaultBatchConfigurer 并提供了数据源。现在它说表不存在。这是否意味着我们需要自己创建表格。我认为不应该是这样,因为它在使用 DefaultBatchConfigurer 之前在其他数据源上自己创建了表。从 BATCH_JOB_INSTANCE 中选择 JOB_INSTANCE_ID、JOB_NAME,其中 JOB_NAME = ?按 JOB_INSTANCE_ID desc 排序];嵌套异常是 java.sql.SQLSyntaxErrorException: ORA-00942: 表或视图不存在
  • 我没有看到您配置要在属性中使用的脚本,因此如果您不自己手动运行它,则需要使用初始化程序功能让 Spring 运行架构脚本给你。
  • 我们如何初始化。我确实有 public void setDataSource(@Qualifier("batchDataSource") DataSource batchDataSource) { super.setDataSource(batchDataSource); } 扩展 DefaultBatchConfigurer 后被覆盖
【解决方案2】:

复制现有数据源属性并覆盖 BatchConfigurer 以返回此新数据源。然后,在新数据源的属性中,更改任一

  1. 连接到数据库的用户使用默认架构定义为 Spring Batch 表的所需架构

  2. 包含 Spring Batch 表所需架构的连接 url。

您选择的选项将取决于您的数据库类型,如下所示:

对于 SQL Server,您可以为用于连接数据库的用户定义默认架构(我做了这个)。

CREATE SCHEMA batchschema;

USE database;
CREATE USER batchuser;
GRANT CREATE TABLE TO batchuser;    
ALTER USER batchuser WITH DEFAULT_SCHEMA = batchschema;
ALTER AUTHORIZATION ON SCHEMA::batchschema TO batchuser;

对于 Postgres 9.4,您可以使用 currentSchema 参数在连接 URL 中指定架构:jdbc:postgresql://host:port/db?currentSchema=batch

对于 9.4 之前的 Postgres,您可以使用 searchpath 参数在连接 URL 中指定架构:jdbc:postgresql://host:port/db?searchpath=batch

对于 Oracle,似乎需要在会话中设置架构。我不确定这个是如何工作的......

ALTER SESSION SET CURRENT_SCHEMA batchschema

限定每个数据源,将您希望用于批处理表的数据源设置为@Primary,并为 DefaultBatchConfigurer 设置数据源,如下所示:

@Bean(name="otherDataSource")
public DataSource otherDataSource() {
    //...
}

@Primary
@Bean(name="batchDataSource")
public DataSource batchDataSource() {
    //...
}

@Bean
BatchConfigurer configurer(@Qualifier("batchDataSource") DataSource dataSource){
    return new DefaultBatchConfigurer(dataSource);
}

【讨论】:

    【解决方案3】:

    application.properties 中的以下属性对我有用。这将在您的数据库中的 new_schema 下创建元模式表。

    spring.batch.tablePrefix=new_schema.BATCH_
    

    下面是我使用的springBoot版本。

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    

    【讨论】:

    • 你能详细说明你的答案吗?请完成代码sn-p
    • 它给了我错误Caused by: java.sql.SQLSyntaxErrorException: Table 'batchmetadata.batch_job_instance' doesn't exist
    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 2010-12-17
    • 2017-02-18
    • 1970-01-01
    • 2015-12-06
    相关资源
    最近更新 更多