Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialise H2 database for spring batch application

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 :)

like image 464
Jérémy Avatar asked Nov 06 '25 06:11

Jérémy


1 Answers

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();
}
like image 197
Monzurul Shimul Avatar answered Nov 08 '25 09:11

Monzurul Shimul