Skip to content

Help users to avoid mismatched DataSource and TransactionManager pairs #5388

Open

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.

Activity

  1. quaff commented on May 6, 2026

    @quaff
    Contributor

    What if provided TransactionManager is not DataSourceTransactionManager ?

  2. wilkinsona commented on May 6, 2026

    @wilkinsona
    MemberAuthor

    That's no different to any other combination. The goal would be for Batch, on a best-effort basis, to determine whether or not the combination is valid and to inform the user if it is not.

  3. DragonFSKY commented on Jun 22, 2026

    @DragonFSKY
    Contributor

    I can take a look at this. I’ll keep the scope focused on a best-effort, warning-only check in JdbcJobRepositoryFactoryBean that compares the configured JobRepository DataSource with a DataSource resource factory exposed by a ResourceTransactionManager, with focused tests for the supported and skipped cases.

  4. added a commit that references this issue on Jun 22, 2026
  5. DragonFSKY commented on Jun 22, 2026

    @DragonFSKY
    Contributor

    I opened PR #5437 with a focused, warning-only implementation.

    The check is intentionally best-effort: it runs in JdbcJobRepositoryFactoryBean, compares the configured JobRepository DataSource with a DataSource resource factory exposed by a ResourceTransactionManager, and skips cases where the resource cannot be determined as a DataSource.

  6. added 2 commits that reference this issue on Aug 25, 2026
  7. added 3 commits that reference this issue on Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions