2

I have newly create springboot batch application with Java 8 and i want to create a database for springbatch tables only with anotation.

I suppose i have to create configuration file but i don't know how to do that.

You can see below all configuration that i want to reproduce in my java program with annotation :

<!-- Base de donnees H2 pour les tables Spring Batch -->
<jdbc:embedded-database id="springBatchDataSource" type="H2">
    <jdbc:script location="org/springframework/batch/core/schema-drop-h2.sql" />
    <jdbc:script location="org/springframework/batch/core/schema-h2.sql" />
</jdbc:embedded-database>

<!-- TransactionManager Spring Batch -->
<bean id="springBatchTransactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

<!-- JobRepository Spring Batch -->
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
    <property name="dataSource" ref="springBatchDataSource" />
    <property name="transactionManager" ref="springBatchTransactionManager" />
    <property name="databaseType" value="H2" />
</bean>

I have add the code below :

@Configuration public class ConfigBatch {

@Bean(destroyMethod = "shutdown")
public EmbeddedDatabase dataSourceH2() {
    return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2)
            .addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
            .addScript("classpath:org/springframework/batch/core/schema-h2.sql").build();
}

@Bean
public SimpleJobLauncher jobLauncher() throws Exception {
    final SimpleJobLauncher launcher = new SimpleJobLauncher();
    launcher.setJobRepository(jobRepository());
    return launcher;
}

@Bean
public JobRepository jobRepository() throws Exception {
    final JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDatabaseType(DatabaseType.H2.getProductName());
    factory.setDataSource(dataSourceH2());
    factory.setTransactionManager(transactionManager());
    return factory.getObject();
}

@Bean
public ResourcelessTransactionManager transactionManager() {
    return new ResourcelessTransactionManager();
}

}

My import "@ImportResource" generate an error because there is one datasource in my java code and one datasource in my xml file :

No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2:

I just want to generate spring batch tables in H2 datasource and run batch writer in oracle datasource (xml import resource).

Can you help me ? Thank you :)

Jérémy
  • 169
  • 4
  • 15

3 Answers3

3

Put the following codes inside a class annotated with @Configuration.

@Bean
public DataSource dataSource() {
    EmbeddedDatabaseBuilder embeddedDatabaseBuilder = new EmbeddedDatabaseBuilder();
    return embeddedDatabaseBuilder.addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
            .addScript("classpath:org/springframework/batch/core/schema-h2.sql")
            .setType(EmbeddedDatabaseType.H2)
            .build();
}

@Bean
public ResourcelessTransactionManager transactionManager() {
    return new ResourcelessTransactionManager();
}

@Bean
public JobRepository jobRepository() throws Exception {
    JobRepositoryFactoryBean factory = new JobRepositoryFactoryBean();
    factory.setDatabaseType(DatabaseType.H2.getProductName());
    factory.setDataSource(dataSource());
    factory.setTransactionManager(transactionManager());
    return factory.getObject();
}
Monzurul Haque Shimul
  • 7,005
  • 2
  • 19
  • 34
0

Configuring a second dataSource for embedded H2 database for Spring Batch Repository, and using primary dataSource for Oracle or another db. Defining a second dataSource bean, and adding it to jobRepository wasn't enough. The spring.batch.initialize-schema=embedded will not initialize this db, since it will try and use the primary dataSource. The following worked for me.

@Configuration
public class H2BatchRepositoryConfigurer extends DefaultBatchConfigurer {
    @Autowired
    @Qualifier("h2DataSource")
    private DataSource dataSource;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Override
    protected JobRepository createJobRepository() throws Exception {
        JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
        factoryBean.setDatabaseType(DatabaseType.H2.getProductName());
        factoryBean.setTablePrefix("BATCH_");
        factoryBean.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
        factoryBean.setDataSource(dataSource);
        factoryBean.setTransactionManager(platformTransactionManager);
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }

    @Override
    protected JobExplorer createJobExplorer() throws Exception {
        JobExplorerFactoryBean factoryBean = new JobExplorerFactoryBean();
        factoryBean.setDataSource(this.dataSource);
        factoryBean.setTablePrefix("BATCH_");
        factoryBean.afterPropertiesSet();
        return factoryBean.getObject();
    }

    @Bean(destroyMethod = "shutdown")
    public EmbeddedDatabase dataSourceH2() {
        return new EmbeddedDatabaseBuilder()
                .setType(EmbeddedDatabaseType.H2)
                .addScript("classpath:org/springframework/batch/core/schema-drop-h2.sql")
                .addScript("classpath:org/springframework/batch/core/schema-h2.sql")
                .build();
    }
}

Follow this link to define h2DataSource bean Spring Boot Configure and Use Two DataSources

saa2k15
  • 25
  • 5
0

configuration class

@EnableBatchProcessing
@Import({ DataSourceConfiguration.class, OracleDbConfig.class })
public class Example BatchConfiguration extends DefaultBatchConfigurer {

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

Next create class for Batch embedded data source

package com.cookmedical.batch.configuration;

import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

@Configuration
public class DataSourceConfiguration {

    @Primary
    @Bean(name = "batchDataSource")
    public DataSource batchDataSource() {
        return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.H2).build();
    }
}

oracle database. update basePackages as your model and JpaRepository

@Configuration
@EnableTransactionManagement
@EnableJpaRepositories(entityManagerFactoryRef = "orcaleEntityManagerFactory", transactionManagerRef = "orcaleTransactionManager", basePackages = {
        "com.test.batch.orcale.repo" })

//@EntityScan( basePackages = {"com.test.batch.dao.entity"} )
public class OracleDbConfig {

    @Bean(name = "dataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "orcaleEntityManagerFactory")
    public LocalContainerEntityManagerFactoryBean orcaleEntityManagerFactory(EntityManagerFactoryBuilder builder,
            @Qualifier("dataSource") DataSource dataSource) {
        return builder.dataSource(dataSource).packages("com.test.batch.orcale.domain").persistenceUnit("orcale")
                .build();
    }

    @Bean(name = "orcaleTransactionManager")
    public PlatformTransactionManager orcaleTransactionManager(
            @Qualifier("orcaleEntityManagerFactory") EntityManagerFactory orcaleEntityManagerFactory) {
        return new JpaTransactionManager(orcaleEntityManagerFactory);
    }

}
package com.test.batch.orcale.repo;

import org.springframework.data.jpa.repository.JpaRepository;

import com.test.batch.orcale.domain.CustomerView;

public interface ICustomerViewRepository   extends JpaRepository<CustomerView, Long>{

    CustomerView findByCustomerNbr(String customerNbr);
}

application.properties file. No need any entry for h2 data source.

spring.datasource.jdbcUrl=jdbc:oracle:thin:@o:1521/
spring.datasource.username=**
spring.datasource.password=*