Quartz Spring Boot There is no DataSource named 'dataSource'

Viewed 838

I have the following quartz.properties:

org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX
org.quartz.jobStore.dataSource=dataSource
org.quartz.jobStore.driverDelegateClass=org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
org.quartz.jobStore.tablePrefix=qrtz_

org.quartz.threadPool.threadCount=1
org.quartz.scheduler.skipUpdateCheck=true
org.quartz.plugin.triggerHistory.class=org.quartz.plugins.history.LoggingTriggerHistoryPlugin

Also, I added QuartzConfiguration:

@Configuration
@EnableScheduling
public class QuartzConfiguration {

    public static final String CONTEXT_KEY = "applicationContext";

    @Autowired
    private DataSource dataSource;

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean scheduler = new SchedulerFactoryBean();
        scheduler.setApplicationContextSchedulerContextKey("applicationContext");
        scheduler.setConfigLocation(new ClassPathResource("quartz.properties"));
        scheduler.setDataSource(dataSource);
        scheduler.setWaitForJobsToCompleteOnShutdown(true);
        return scheduler;
    }

}

In the application.properties I have defined:

#PostgreSQL
spring.datasource.url=${postgresql.datasource.url}
spring.datasource.username=${postgresql.datasource.username}
spring.datasource.password=${postgresql.datasource.password}

Right now, during start up the application fails with the following exception:

Caused by: org.quartz.JobPersistenceException: Failed to obtain DB connection from data source 'dataSource': java.sql.SQLException: There is no DataSource named 'dataSource' [See nested exception: java.sql.SQLException: There is no DataSource named 'dataSource']
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.getConnection(JobStoreSupport.java:783)
    at org.quartz.impl.jdbcjobstore.JobStoreTX.getNonManagedTXConnection(JobStoreTX.java:71)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.executeInNonManagedTXLock(JobStoreSupport.java:3861)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.recoverJobs(JobStoreSupport.java:839)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.schedulerStarted(JobStoreSupport.java:695)
    ... 45 more
Caused by: java.sql.SQLException: There is no DataSource named 'dataSource'
    at org.quartz.utils.DBConnectionManager.getConnection(DBConnectionManager.java:104)
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.getConnection(JobStoreSupport.java:780)
    ... 49 more

What am I doing wrong and how to provide a correct DataSource to the org.quartz.jobStore.dataSource ?

1 Answers

Two options:

  1. create a configuration bean that specifically creates you DataSource.

         @Bean
         public DataSource getDataSource()
         {
             DataSourceBuilder dataSourceBuilder = DataSourceBuilder.create();
             dataSourceBuilder.driverClassName("org.h2.Driver");
             dataSourceBuilder.url("jdbc:h2:file:C:/temp/test");
             dataSourceBuilder.username("sa");
             dataSourceBuilder.password("");
             return dataSourceBuilder.build();
         }
    
  2. You can alternatively use JNDI, use the spring.datasource.jndi-name

see also

Related