Spring batch tables in a different schema

后端 未结 3 1087
眼角桃花
眼角桃花 2021-01-20 05:13

I want to use a different schema to save Spring Batch tables. I can see that my new datasource in set in the JobRepositoryFactoryBean. But still the tables are

相关标签:
3条回答
  • 2021-01-20 05:51

    Below property in application.properties is working for me.This will create meta schema tables under new_schema in your DB.

    spring.batch.tablePrefix=new_schema.BATCH_
    

    Below is the version of springBoot I am using.

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.3.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
    
    0 讨论(0)
  • 2021-01-20 05:58

    Duplicate your existing data source properties and override BatchConfigurer to return this new data source. Then, in the new data source's properties, change either

    1. The user connecting to the database to one with a default schema defined as the desired schema for the Spring Batch tables

    2. The connection url to include the desired schema for the Spring Batch tables.

    The option you choose will depend on your database type as follows:

    For SQL Server you can define the default schema for the user you are using to connect to the database (I did this one).

    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;
    

    For Postgres 9.4 you can specify schema in the connection URL using currentSchema parameter: jdbc:postgresql://host:port/db?currentSchema=batch

    For Postgres before 9.4 you can specify schema in the connection URL using searchpath parameter: jdbc:postgresql://host:port/db?searchpath=batch

    For Oracle it looks like the schema would need to be set on the session. I'm not exactly sure how this one would work...

    ALTER SESSION SET CURRENT_SCHEMA batchschema
    

    Qualify each DataSource, set one you wish to use for the Batch tables as @Primary, and set your datasource for the DefaultBatchConfigurer as follows:

    @Bean(name="otherDataSource")
    public DataSource otherDataSource() {
        //...
    }
    
    @Primary
    @Bean(name="batchDataSource")
    public DataSource batchDataSource() {
        //...
    }
    
    @Bean
    BatchConfigurer configurer(@Qualifier("batchDataSource") DataSource dataSource){
        return new DefaultBatchConfigurer(dataSource);
    }
    
    0 讨论(0)
  • 2021-01-20 06:10

    When using Spring Batch's @EnableBatchProcessing, the DataSource used by the Spring Batch tables is the one provided by the BatchConfigurer. If you are using more than one DataSource in your application, you must create your own BatchConfigurer (either by extending DefaultBatchConfigurer or implementing the interface) so that Spring Batch knows which to use. You can read more about this customization in the reference documentation here: https://docs.spring.io/spring-batch/4.0.x/reference/html/job.html#configuringJobRepository

    0 讨论(0)
提交回复
热议问题