How to avoid Spring batch persistence of metadata in DB

后端 未结 1 561
猫巷女王i
猫巷女王i 2021-01-16 00:23

I have a Spring batch job that reads from a DB and writes into a CSV. The batch job is trying to use the DB from which I am reading to save the status of the batch processin

相关标签:
1条回答
  • 2021-01-16 00:37

    As far as I know, disabling metadata persistence is not possible. A possible workaround for not having to setup a 'proper' database is to use an in-memory database for the metadata:

    pom.xml

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
        </dependency>
    

    However, there is the problem of using the default Spring Datasource for the Spring Batch Jobs Metadata repository. Here there is a complete workaround in order to define a secondary datasource for it:

    application.properties

    ###################################
    ### JOBS DATASOURCE PROPERTIES. ###
    ###################################
    ## URL used to connect to the jobs database.
    spring.secondDatasource.url=jdbc:h2:mem:jobsdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
    ## Driver class used to connect to the jobs database (it will depend on datasource).
    spring.secondDatasource.driver-class-Name=org.h2.Driver
    ## User name 
    spring.secondDatasource.username=xxx
    ## Password 
    spring.secondDatasource.password=xxx
    ## Datasource configuration for jobs database.
    spring.jpa.hibernate.ddl-auto=create-drop
    spring.secondDatasource.initialize=true
    spring.secondDatasource.test-on-borrow=true
    spring.secondDatasource.validation-query=select 1
    

    Spring Configuration (I) Datasources

    /**
     * Config class holding several datasources, one business related, other for the spring batch jobs
     */
    @Configuration
    public class DataSourceConfiguration
    {
        @Bean
        @Qualifier("businessDataSource")
        @ConfigurationProperties(prefix = "spring.datasource")
        public DataSource primaryDataSource()
        {
            return DataSourceBuilder.create().build();
        }
    
        @Bean
        @Primary
        @Qualifier("jobsDataSource")
        @ConfigurationProperties(prefix = "spring.secondDatasource")
        public DataSource secondaryDataSource()
        {
            return DataSourceBuilder.create().build();
        }
    }
    

    Spring Configuration (II) - Spring batch

    import org.springframework.batch.core.configuration.annotation.DefaultBatchConfigurer;
    
    // Another @Configuration class...
    
    @Autowired
    @Qualifier("jobsDataSource")
    private DataSource dataSource;
    
    @Bean
    public BatchConfigurer configurer()
    {
        // This is required to avoid problems when jobs datasource is into some secondary datasource.
        return new DefaultBatchConfigurer(dataSource);
    }
    

    With all this, Spring Batch will use the in-memory datasource, while you are free to use the default datasource for your own purposes

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