6

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 ?

alexanoid
  • 24,051
  • 54
  • 210
  • 410
  • 4
    Does reading through https://github.com/spring-projects/spring-framework/issues/27709 help? – Andy Wilkinson May 29 '22 at 08:13
  • 3
    Yes, thank you very much! I removed `org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX` and now it starting fine! – alexanoid May 29 '22 at 08:21
  • This config works with older version of spring boot (2.5.0), later versions won't work. I was using spring boot 2.6.7 and I struggled for couple of hours to figure this out, later with the above github link I got the resolution. Below is the property needed to be changed - form this - org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX to this - org.quartz.jobStore.class=org.springframework.scheduling.quartz.LocalDataSourceJobStor – Sachchidanand Singh Dec 18 '22 at 11:45

1 Answers1

0

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

  • 4
    Thanks, but datasource is already created and accesible by `@Autowired private DataSource dataSource;` – alexanoid May 29 '22 at 07:48