简体   繁体   中英

org.quartz.SchedulerConfigException: DataSource name not set

After upgrading spring-boot(from 2.3.12.RELEASE to 2.7.6 ) and spring cloud(from 3.0.3 to 3.1.5 ) I started to receive follwing error

Caused by: org.quartz.SchedulerConfigException: DataSource name not set.
    at org.quartz.impl.jdbcjobstore.JobStoreSupport.initialize(JobStoreSupport.java:643)
    at org.quartz.impl.jdbcjobstore.JobStoreTX.initialize(JobStoreTX.java:57)
    at org.quartz.impl.StdSchedulerFactory.instantiate(StdSchedulerFactory.java:1368)
    at org.quartz.impl.StdSchedulerFactory.getScheduler(StdSchedulerFactory.java:1579)
    at org.springframework.scheduling.quartz.SchedulerFactoryBean.createScheduler(SchedulerFactoryBean.java:679)
    at org.springframework.scheduling.quartz.SchedulerFactoryBean.prepareScheduler(SchedulerFactoryBean.java:616)
    at org.springframework.scheduling.quartz.SchedulerFactoryBean.afterPropertiesSet(SchedulerFactoryBean.java:504)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1863)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800)

Based on this advice https://github.com/spring-projects/spring-framework/issues/27709#issuecomment-988526589

I've added

org.quartz.jobStore.class: org.quartz.impl.jdbcjobstore.JobStoreTX

to my application.yml

but I still experienxe this error during tests execution.

my dependencies:

</properties>
   <spring-boot.version>2.7.6</spring-boot.version>
   ...
</properties>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
 ...
<dependencyManagement>
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-validation</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-oauth2-client</artifactId>
      <exclusions>
        <exclusion>
          <groupId>com.nimbusds</groupId>
          <artifactId>oauth2-oidc-sdk</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-security</artifactId>
    </dependency>

    <!--         tracing -->
    <dependency>
      <groupId>io.opentracing.contrib</groupId>
      <artifactId>opentracing-spring-jaeger-web-starter</artifactId>
    </dependency>

    <!-- for quartz logic -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-quartz</artifactId>
    </dependency>

    <!-- actuator & metric-->
    <dependency>
      <groupId>io.micrometer</groupId>
      <artifactId>micrometer-registry-prometheus</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

 <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-bus-kafka</artifactId>
    </dependency>

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-context</artifactId>
    </dependency>

    <!-- test -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <scope>test</scope>
    </dependency>
    ...

How can I fix?

Update

I also tried to add to my application yaml:

spring:
  quartz:
    properties:
      org:
        quartz:
          jobStore:
            class: org.springframework.scheduling.quartz.LocalDataSourceJobStore
            driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
            tablePrefix: TB_PR_QRTZ_

and marked this beasn with @QuartzDataSource

@Bean
@QuartzDataSource
public DataSource testDataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(env.getProperty("spring.datasource.driverClassName"));
    dataSource.setUrl(TestContainersRunner.JDBC_URL);
    dataSource.setUsername(env.getProperty("spring.datasource.username"));
    dataSource.setPassword(env.getProperty("spring.datasource.password"));
    return dataSource;
}

But still no luck

Try tagging your bean as @Primary .

If it doesnt work I or if you have multiple datasource beans, I provide below a solution for that specific case that worked for me.

How to set dataSource in quartz schedular. [ERROR] org.quartz.SchedulerException: Could not initialize DataSource: myDS

Basicaly instead of providing a spring datasource property, you define your datasource as a quartz specific datasource

spring:
  datasource:
    byq:
      url: jdbc:oracle:thin:@//app-db/schema-name
      driverClassName: oracle.jdbc.OracleDriver
      username: ZZZ
      password: XXX
  quartz:
    job-store-type: jdbc
    wait-for-jobs-to-complete-on-shutdown: true
    jdbc:
      initialize-schema: never
    properties:
      org:
        quartz:
          dataSource:
            quartzDataSource:
              URL: jdbc:postgresql://scheduler-db/scheduler
              driver: org.postgresql.Driver
              user: ZZZ
              password: XXX
          jobStore:
            dataSource: quartzDataSource
            class: org.quartz.impl.jdbcjobstore.JobStoreTX
            driverDelegateClass: org.quartz.impl.jdbcjobstore.PostgreSQLDelegate
            tablePrefix: TB_QRTZ_

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM