5

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 been created in the other shcema where I have business tables. I read soemwhere that I can use dataSource.setValidationQuery to alter the schema, but still doesn't work. I can solve this. Below is the JobRepositoryFactoryBean and Datasource prop.

 @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());
Prashant Pokhriyal
  • 3,727
  • 4
  • 28
  • 40
user700
  • 111
  • 1
  • 3
  • 11

3 Answers3

5

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>
T D
  • 1,080
  • 2
  • 13
  • 20
  • Could you please elaborate your answer ? Please complete code snippet – PAA Mar 27 '19 at 09:51
  • Its giving me error `Caused by: java.sql.SQLSyntaxErrorException: Table 'batchmetadata.batch_job_instance' doesn't exist` – PAA Dec 31 '19 at 13:11
2

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

Michael Minella
  • 20,843
  • 4
  • 55
  • 67
  • 2
    Ok thanks. So now I extended DefaultBatchConfigurer and provided the datasource. Now it says TABLE DOES NOT EXIST. Does that mean we need to create tabled by ourself. I think it shouldn't be the case as it created tables by its own on the other datasource before using DefaultBatchConfigurer . SELECT JOB_INSTANCE_ID, JOB_NAME from BATCH_JOB_INSTANCE where JOB_NAME = ? order by JOB_INSTANCE_ID desc]; nested exception is java.sql.SQLSyntaxErrorException: ORA-00942: table or view does not exist – user700 Nov 01 '17 at 19:00
  • I don't see you configuring the script to be used in your properties so if you don't run it yourself manually, you'd need to use the initializer functionality to have Spring run the schema script for you. – Michael Minella Nov 01 '17 at 20:17
  • 1
    how do we initialize. I do have public void setDataSource(@Qualifier("batchDataSource") DataSource batchDataSource) { super.setDataSource(batchDataSource); } which is overridden after extending DefaultBatchConfigurer – user700 Nov 01 '17 at 21:34
0

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);
}
user3474985
  • 983
  • 8
  • 20