简体   繁体   English

具有Spring数据的Spring Boot批处理将元数据写入不同的架构(在内存中:HSQL或H2)

[英]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 : 我正在使用以下技术编写一个批处理:Spring Boot运行该应用程序:V1.5.3.RELEASE带有Spring Batch Config的Spring Batch:spring-batch-infrastructure V3.0.7.RELEASE将我的通用DAO的Spring Data传递到业务数据库: > spring-data-jpa V1.11.3.RELEASE我到oracle datbase的数据源是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. 我想将元数据写入另一个数据库中(例如,在内存HSQL或H2中),但是我找不到方法,因为上下文正在将元数据写入同一数据库中。 The only way is to define a TransactionManager and an EntityManager and enable them to my DAO : 唯一的方法是定义一个TransactionManager和一个EntityManager并将它们启用到我的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: 在我在app.properties中定义spring默认数据源后,它可以这样工作:

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: 我什至尝试制作一个自定义的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 : 在我禁用默认数据源的spring数据的初始化之后,它仅适用于我的元数据,但在oracle业务数据库中未写入任何内容:

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. 您将需要创建BatchConfigurer的自定义实现(通常通过扩展DefaultbatchConfigurer 。这将允许您显式配置批处理DataSource

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 您可以在以下文档中阅读有关BatchConfigurer更多信息: http : //docs.spring.io/spring-batch/apidocs/org/springframework/batch/core/configuration/annotation/BatchConfigurer.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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