1

I am writing a Spring Batch job and don't care about restarts and don't want the hassle of creating, securing and managing an Oracle schema for Spring Batch database objects. H2 on the file system is more than enough for us.

The issue is that I am writing an batch job that needs to connect to an Oracle database and failing miserably just trying to get 2 data sources, transaction managers and entity managers. Right now I am trying two H2 database and haven't even tried configuring Oracle database yet.

I have used two Oracle data sources in other Spring Boot applications successfully in the past but this is my first attempt with Spring Batch that has configuration code to create the data source.

I have tried creating two DataSource, EntityManagerFactoryBean and TransactionManager with one using default spring.datasources configuration, default bean names and @Primary.

I have tried creating just a second DataSource, EntityManagerFactoryBean and TransactionManager with different bean names. This seems to have an issue that the TransactionManager orEntityManager is already assigned to the thread.

I have tried creating a dataSource for batch but run into circular bean creation errors.

I have tried creating a BatchConfigurer and that runs into circular bean creation errors.

I have tried creating a JobRepositoryFactoryBean but a default one is still created.

I have tried using a @PersistenceContext on my @Entity classes.

It shouldn't be this hard so I must be missing something. Any help would be appreciated.

Thanks, Wes.

Wes
  • 847
  • 2
  • 10
  • 22
  • Possible duplicate of [Use of multiple DataSources in Spring Batch](https://stackoverflow.com/questions/25540502/use-of-multiple-datasources-in-spring-batch) – Mahmoud Ben Hassine Oct 30 '19 at 20:13
  • Thanks. That is a different error but I read through it. The only thing new was defining the `DefaultBatchConfigurer` `@Component`. I tried that but it did not change my outcome since I was able to remove it and still get eventual "success". – Wes Oct 31 '19 at 19:56

1 Answers1

1

I think I have some success. I am able to eventually run the batch job using Spring Boot 2.2.0 and Spring Batch 4.2.0 but I have to wait for a 5 minute timeout while creating the Entity Manager for H2 for the Spring Batch repository. The Oracle Entity Manager is registered really quickly and before the H2 Entity Manager despite the H2 Entity Manager being @Primary.

I have a separate configuration class for each of my two DataSources. Each one is annotated with @Configuration, @EnableTransactionManagement and @EnableJPARepository.

The one for Spring Batch is using the standard bean names, dataSource, entityManagerFactory and transactionManager. Each @Bean is annotated with @Primary. One setting that I needed was to add a .packages("org") to the entityManagerFactory bean. This would pick up all of org.springframework including Spring Batch. The only other real change I have made from common implementations is to set the dialect in the JPA Properties of the Entity Manager.

I needed the spring.main.allow-bean-definition-overriding: true setting.

There may be more to my solution that I should share but I have been at this for a couple of days and have gone in circles. I even remember getting what appeared as a hung process time and and killed the job thinking it was hung. I may have had some "success" early on but just was too quick to kill the execution.

I would still like to know why it is taking so long to create the H2 Entity Manager.

Thanks, Wes.

Wes
  • 847
  • 2
  • 10
  • 22