简体   繁体   中英

Create metadata tables for Spring Batch in an embedded database

I'm using Spring Boot autoconfigured Spring Batch setup with @EnableBatchProcessing . The problem is that it creates metadata tables in the main database and I don't want this behavior. I would like to save all the Spring Batch information to an embedded database.

I've tried using the spring.batch.initialize-schema=embedded property and adding H2 to the classpath, overriding DefaultBatchConfigurer bean with the H2 data source, replacing JobRepository and JobLauncher beans but it constantly creates metadata tables in the main Oracle database. I'm using Spring Batch 3.0.8 and Spring Boot 1.5.9.

Any help is appreciated, thank you!

UPDATE : Adding come configuration:

H2 config:

@Bean
public DataSource springBatchDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("org.h2.Driver");
    dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
    dataSource.setUsername("sa");
    dataSource.setPassword("sa");

    return dataSource;
}

Oracle:

@Bean
@Primary
public DataSource dataSource() throws SQLException {
    PoolDataSourceImpl dataSource = new PoolDataSourceImpl();
    dataSource.setConnectionFactoryClassName(environment.getRequiredProperty("db.driverClassName"));
    dataSource.setURL(environment.getRequiredProperty("db.url"));
    dataSource.setUser(environment.getRequiredProperty("db.username"));
    dataSource.setPassword(environment.getRequiredProperty("db.password"));
    dataSource.setFastConnectionFailoverEnabled(Boolean.valueOf(environment
            .getRequiredProperty("db.fast.connect.failover.enabled")));
    dataSource.setValidateConnectionOnBorrow(true);
    dataSource.setSQLForValidateConnection("SELECT SYSDATE FROM DUAL");
    dataSource.setONSConfiguration(environment.getRequiredProperty("db.ons.config"));
    dataSource.setInitialPoolSize(Integer.valueOf(environment.getRequiredProperty("db.initial.pool.size")));
    dataSource.setMinPoolSize(Integer.valueOf(environment.getRequiredProperty("db.min.pool.size")));
    dataSource.setMaxPoolSize(Integer.valueOf(environment.getRequiredProperty("db.max.pool.size")));
    dataSource.setAbandonedConnectionTimeout(120);
    dataSource.setInactiveConnectionTimeout(360);
    dataSource.setTimeToLiveConnectionTimeout(0);

    return dataSource;
}

Batch configuration:

@Configuration
@EnableBatchProcessing
public class BatchConfigurer extends DefaultBatchConfigurer {

    @Override
    @Autowired
    public void setDataSource(@Qualifier("springBatchDataSource") DataSource dataSource) {
        super.setDataSource(dataSource);
    }

}

and some properties related..

spring:
  batch:
    job.enabled: false

1.redefine the BasicBatchConfigurer

2.in spring-boot-batch-starter, you must create your own BatchDataSourceInitializer , so it will execute the init sql scripts

@Configuration
public class MyBatchConfigurer {

    @Bean
    public BasicBatchConfigurer batchConfigurer(BatchProperties properties,
                @Qualifier("springBatchDataSource") DataSource dataSource,
                ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers) {
            return new BasicBatchConfigurer(properties, dataSource,
                    transactionManagerCustomizers.getIfAvailable());
        }

    @Bean
    public BatchDataSourceInitializer batchDataSourceInitializer(@Qualifier("springBatchDataSource") DataSource dataSource,
            ResourceLoader resourceLoader, BatchProperties properties) {
        return new BatchDataSourceInitializer(dataSource, resourceLoader,
                properties);
    }
}

You need to qualify your H2 datasource bean

  @Bean(name = "springBatchDataSource")
    public DataSource springBatchDataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName("org.h2.Driver");
        dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
        dataSource.setUsername("sa");
        dataSource.setPassword("sa");

        return dataSource;
    }

I hope this help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM