Skip to content

JdbcStoreTypeConfiguration picks transaction of primary data source when Quartz data source is not the primary data source #20184

@breun

Description

@breun

QuartzAutoConfiguration.JdbcStoreTypeConfiguration will use the transaction manager of the primary data source when an application has multiple data sources and a non-primary data source is annotated with @QuartzDataSource.

Consider this configuration of beans:

@Primary
@Bean
public HikariDataSource primaryDataSource() {
    HikariConfig config = new HikariConfig();
    config.setJdbcUrl("jdbc:postgresql://localhost:5432/postgres");
    config.setDriverClassName("org.postgresql.Driver");
    config.setUsername("postgres");
    config.setPassword("mysecretpassword");
    config.setAutoCommit(false);
    config.setIdleTimeout(10000);
    config.setPoolName("Primary pool");
    return new HikariDataSource(config);
}

@Primary
@Bean
public DataSourceTransactionManager primaryDataSourceTransactionManager() {
    return new DataSourceTransactionManager(primaryDataSource());
}

@QuartzDataSource
@Bean
public HikariDataSource quartzDataSource() {
    HikariConfig config = new HikariConfig();
    config.setJdbcUrl("jdbc:postgresql://localhost:5432/quartzexample");
    config.setDriverClassName("org.postgresql.Driver");
    config.setUsername("quartzexample");
    config.setPassword("quartzexamplepw");
    config.setAutoCommit(false);
    config.setIdleTimeout(10000);
    config.setPoolName("Quartz pool");
    return new HikariDataSource(config);
}

@Bean
public DataSourceTransactionManager quartzDataSourceTransactionManager() {
    return new DataSourceTransactionManager(quartzDataSource());
}

In org.springframework.boot.autoconfigure.quartz.QuartzAutoConfiguration.JdbcStoreTypeConfiguration#dataSourceCustomizer you will see the transaction manager for the primary datasource gets set on the SchedulerFactoryBean.

In this scenario actually the transaction manager of the datasource annotated with @QuartzDataSource should be set on the SchedulerFactoryBean. As a workaround this can be accomplished by adding a SchedulerFactoryBeanCustomizer like this:

@Order(Ordered.HIGHEST_PRECEDENCE)
@Bean
public SchedulerFactoryBeanCustomizer schedulerFactoryBeanCustomizer() {
    return schedulerFactoryBean -> schedulerFactoryBean.setTransactionManager(quartzDatasourceTransactionManager());
}

But ideally auto-configuration would take of this automatically of course.

This is using Spring Boot 2.2.4.RELEASE.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions