Quartz Spring Boot There is no DataSource named 'dataSource'
Asked Answered
B

2

7

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 ?

Bedplate answered 29/5, 2022 at 7:18 Comment(3)
Does reading through github.com/spring-projects/spring-framework/issues/27709 help?Advection
Yes, thank you very much! I removed org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX and now it starting fine!Bedplate
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.LocalDataSourceJobStorTannic
U
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

Understood answered 29/5, 2022 at 7:46 Comment(2)
Thanks, but datasource is already created and accesible by @Autowired private DataSource dataSource;Bedplate
@Bedplate / @Yoav should we need add the implementation for DataSource initialization? as we are already having @Autowired private DataSource dataSource;Athena
A
0

As @alexanoid mentioned after removing org.quartz.jobStore.class=org.quartz.impl.jdbcjobstore.JobStoreTX, it is working as expected.

In Quartz properties file:

org.quartz.scheduler.instanceName = Scheduler
org.quartz.scheduler.instanceId = AUTO

org.quartz.threadPool.class = org.quartz.simpl.SimpleThreadPool
org.quartz.threadPool.threadCount = 5
org.quartz.threadPool.threadPriority = 5

org.quartz.jobStore.isClustered = true
org.quartz.jobStore.tablePrefix = QRTZ_
org.quartz.jobStore.driverDelegateClass =                 
org.quartz.impl.jdbcjobstore.StdJDBCDelegate

In application.properties file,

spring.datasource.url=DBURL
spring.datasource.username=USERNAME
spring.datasource.password=PASSWORD
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
@Configuration
public class QuartzConfig {

    @Autowired
    private DataSource dataSource;
    
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean() {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setDataSource(dataSource);
        schedulerFactoryBean.setConfigLocation(new ClassPathResource("quartz.properties"));
        return schedulerFactoryBean;
    }

    @Bean
    public Scheduler scheduler() {
        SchedulerFactoryBean schedulerFactoryBean = schedulerFactoryBean();
        return schedulerFactoryBean.getScheduler();
    }
}
Athena answered 27/5 at 7:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.