membersound
membersound

Reputation: 86747

Spring-Batch how to write job metdata in different database?

I'm using Spring-Batch to persist lots of data in a database using batch JpaItemWriter<T>.

I would like to have the job-metadata that is autogenerated by spring to be written into a different database (an embedded h2 would be sufficient) than the data that is written by the ItemWriter.

Is that possible at all? Where would I have to set the different DataSource or TransactionManager so that the job-metadata gets persisted?

Upvotes: 2

Views: 2558

Answers (2)

membersound
membersound

Reputation: 86747

It turned out that my configuration was already working, but spring-boot automatically loads them schema-{db}.sql scripts on startup. Thus I always had the metadata in my main DB.

Solution is a simple property:

spring.batch.initializer.enabled=false

Upvotes: 3

gyoder
gyoder

Reputation: 4738

Absolutely, but as soon as you create a second DataSource you're going to have to autowiring problems because Spring will start finding two candidate beans. Here's how you can get around this:

(1) Mark you current datasource bean with @Primary (you will need to create this bean explicitly if you're not already doing so)

    @Bean(name = "mainDataSource")
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource") //assuming connection, credentials configured in application.properties
    public DataSource createDataSource() {
        return DataSourceBuilder.create().build();
    }

(2) Create your new datasource (same as above) for you job meta-data, and give it a name.

(3) Use an @Qualifier to autowire this new datasource in the repository you use to persist your job meta-data.

Upvotes: 1

Related Questions