Spring batch tables in a different schema

后端 未结 3 1117
眼角桃花
眼角桃花 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: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);
    }
    

提交回复
热议问题