Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Help users to avoid mismatched DataSource and TransactionManager pairs #5388

@wilkinsona

Description

@wilkinsona

Expected Behavior

When using its JDBC support, Batch should do what it can to protect against someone misconfiguring the DataSource and TransactionManager. If Batch can identify that its DataSource is not the same as its TransactionManager's DataSource, it should log a warning or even fail fast when starting up.

Current Behavior

The misconfiguration is ignored.

Context

I'd like an arrangement like this to fail or at least log a warning:

@EnableBatchProcessing
@EnableJdbcJobRepository(dataSourceRef = "firstDataSource")
public class BrokenTransactionConfigurationApplication {

	private final DataSource firstDataSource = new EmbeddedDatabaseBuilder().build();

	private final DataSource secondDataSource = new EmbeddedDatabaseBuilder().build();

	@Bean
	DataSource firstDataSource() {
		return new EmbeddedDatabaseBuilder().build();
	}

	@Bean
	DataSource secondDataSource() {
		return new EmbeddedDatabaseBuilder().build();
	}

	@Bean
	DataSourceTransactionManager firstTransactionManager() {
		return new DataSourceTransactionManager(this.firstDataSource);
	}

	@Bean
	DataSourceTransactionManager transactionManager() {
		return new DataSourceTransactionManager(this.secondDataSource);
	}

	public static void main(String[] args) {
		SpringApplication.run(BrokenTransactionConfigurationApplication.class, args);
	}

}

You could also get into a similar state by sub-classing JdbcDefaultBatchConfiguration and overriding getDataSource() and/or getTransactionManager(). For Spring Boot users, @BatchDataSource and @BatchTransactionManager are another way.

I think all these approaches end up configuring a JdbcJobRepositoryFactoryBean. Hopefully that provides a central place where some validation of the configured DataSource and TransactionManager could be done to catch misconfigurations early and irrespective of how they arose.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions