Skip to content

Autoconfiguration issue with two DataSources #4688

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
pnavato opened this issue Oct 23, 2024 · 2 comments
Open

Autoconfiguration issue with two DataSources #4688

pnavato opened this issue Oct 23, 2024 · 2 comments
Labels
status: waiting-for-triage Issues that we did not analyse yet

Comments

@pnavato
Copy link

pnavato commented Oct 23, 2024

I converted a simple batch app from v4 to v5. It has a single step that reads from Oracle and writes a CSV; it was using the map-based JobRepository so now I configured it on H2 as follows:

	@BatchDataSource
	@Bean
	public DataSource h2Datasource() {
		EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
		EmbeddedDatabase embeddedDatabase = builder
				.addScript("classpath:org/springframework/batch/core/schema-h2.sql")
				.setType(EmbeddedDatabaseType.H2)
				.build();
		return embeddedDatabase;
	}

	@Bean
	@Primary
	@ConfigurationProperties("spring.datasource")
	public DataSourceProperties oracleDatasourceProperties() {
		return new DataSourceProperties();
	}
	 
	@Bean
	@Primary
	@ConfigurationProperties("spring.datasource.hikari")
	public DataSource oracleDatasource() {
		return oracleDatasourceProperties().initializeDataSourceBuilder().build();
	}

I used neither @EnableBatchProcessing or DefaultBatchConfiguration and apparently everything worked but I noticed that the new batch was using two Oracle connections simultaneously instead of one. P6spy revealed that the 2nd connection was continuosly opened, committed and closed without writing anything. Finally I realized that the autoconfiguration created a TrasactionManager for the Oracle (primary) DataSource and used it to create the H2 JobRepository! So I solved creating it manually:

    @Bean
    public DataSourceTransactionManager h2TransactionManager() {
        return new DataSourceTransactionManager(h2Datasource());
    }

I wonder if autoconfiguration could be made smarter to avoid this kind of problem.

I know that you are going to release a ResourcelessJobRepository (#4679), so my use case will have a better solution but this misconfiguration can also happen if you want to put Spring-Batch tables in a dedicated Oracle schema.

Fell free to close this issue if you think that nothing can be improved on your side, however I hope that my experience will help someone else.

In any case, I think that documenting the two-DataSource setup would be useful. There is already a specific issue for this problem (#4675).

@pnavato pnavato added the status: waiting-for-triage Issues that we did not analyse yet label Oct 23, 2024
@Solodye
Copy link
Contributor

Solodye commented Nov 4, 2024

I wonder if I can first use TestContainers to mock this situation in the example package or UT.

@fmbenhassine
Copy link
Contributor

@pnavato Sure! I will add an example with two data sources to the samples suite.

@Solodye Yes, and that is a good idea! Contributions are welcome.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
status: waiting-for-triage Issues that we did not analyse yet
Projects
None yet
Development

No branches or pull requests

3 participants