Initialise H2 database for spring batch application
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();
}
Related videos on Youtube
Jérémy
Updated on June 04, 2022Comments
-
Jérémy almost 2 years
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 :)
-
Alien over 5 yearsstackoverflow.com/a/9171789/6572971 might help you.
-
Jérémy over 5 yearsThanks. :) But how can define this datasource only for springbatch because i have an error : No qualifying bean of type [javax.sql.DataSource] is defined: expected single matching bean but found 2:
-
Mahmoud Ben Hassine over 5 yearswhich version of spring batch do you use?
-
Jérémy over 5 yearsI use spring-boot-starter-batch 1.4.0.RELEASE (include spring-batch-core 3.0.7.RELEASE)
-
Mahmoud Ben Hassine over 5 yearsok thanks. The answer by @mhshimul is correct. This also may help: stackoverflow.com/a/26531914/5019386.
-
-
fuat over 2 yearsDoes not persist ICustomerViewRepository save method?