Spring Boot Batch With Spring Data Write meta data in different Schema (in memory: HSQL or H2)

时间:2017-08-04 12:19:22

标签: java spring-boot spring-data-jpa metadata spring-batch

I am writring a batch using the following thecnologies: Spring Boot to run the application : V1.5.3.RELEASE Spring Batch with Spring Batch Config: spring-batch-infrastructure V3.0.7.RELEASE Spring Data for my generic DAO to the business database: >spring-data-jpa V1.11.3.RELEASE My datasource to oracle datbase is HikariDataSource :

    @Qualifier("dataSource")
@Bean(destroyMethod = "close")
@Primary
public HikariDataSource dataSource() throws SQLException {
    return buildDataSource();
}

    public HikariDataSource buildDataSource() throws SQLException {
    HikariDataSource ds = new HikariDataSource();
    ds.setMaximumPoolSize(poolSize);
    ds.setDriverClassName(driverClassName);
    ds.setJdbcUrl(jdbcUrl);
    ds.setUsername(userName);
    ds.setPassword(password);
    ds.setConnectionTestQuery("SELECT 1 from DUAL");

    ds.addDataSourceProperty("hibernate.show_sql", showSQL);
    ds.addDataSourceProperty("hibernate.use_sql_comments", useSQLComment);
    ds.addDataSourceProperty("hibernate.format_sql", formatSQL);
    ds.addDataSourceProperty("hibernate.ddl-auto", "none");



    return ds;
}

I want to write my meta data in another database (in memory HSQL or H2 for example) but i can't find a way because the context is writing the meta data in the same database. The only way is to define a TransactionManager and an EntityManager and enable them to my DAO :

@Bean
PlatformTransactionManager businessTransactionManager() throws SQLException {
    return new JpaTransactionManager(businessEntityManagerFactory().getObject());
}

@Bean
LocalContainerEntityManagerFactoryBean businessEntityManagerFactory() throws SQLException {

    HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setGenerateDdl(true);

    LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();

    factoryBean.setDataSource(dataSource());
    factoryBean.setJpaVendorAdapter(jpaVendorAdapter);
    factoryBean.setPackagesToScan("package.of.business.model", "package.of.business.data");

    return factoryBean;
}

and in my batch configuration i add :

@EnableJpaRepositories(entityManagerFactoryRef = "businessEntityManagerFactory",
    transactionManagerRef = "businessTransactionManager", basePackages = {"package.of.business.model",
    "package.of.business.data"})

This way it works after i define the spring default dataSource in my app.properties:

spring.datasource.url=jdbc:h2:mem:test
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.hibernate.ddl-auto=update

What i really want to do is the exact opposite of this, i want that the default database is the business one and i want to override the datasource that writes the meta data but i can't find a way. I even tried to make a custom BatchConfigurer:

    CustomBatchConfigurer extends DefaultBatchConfigurer

It works only for my meta data after i disable the initialization of my spring data for the default datasource but it doesn't write anything in my oracle business database :

batch.data.source.init=false
spring.batch.initializer.enabled=false
spring.batch.initialize.enabled=false
spring.datasource.initialize=false
spring.datasource.continue-on-error=true

Does any one have any idea how this could be done?

1 个答案:

答案 0 :(得分:0)

您需要创建BatchConfigurer的自定义实施(通常通过扩展DefaultbatchConfigurer。这样您就可以明确配置批DataSource

您可以在此处的文档中详细了解BatchConfigurerhttp://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/configuration/annotation/BatchConfigurer.html