简体   繁体   中英

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

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?

You'll need to create a custom implementation of the BatchConfigurer (typically by extending DefaultbatchConfigurer . That will allow you to configure the batch DataSource explicitly.

You can read more about the BatchConfigurer in the documentation here: http://docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/configuration/annotation/BatchConfigurer.html

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