diff --git a/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_6_0/4617-quartz-scheduler-race-condition.yaml b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_6_0/4617-quartz-scheduler-race-condition.yaml new file mode 100644 index 00000000000..0ccabf7c221 --- /dev/null +++ b/hapi-fhir-docs/src/main/resources/ca/uhn/hapi/fhir/changelog/6_6_0/4617-quartz-scheduler-race-condition.yaml @@ -0,0 +1,6 @@ +--- +type: fix +issue: 4617 +title: "Previously, Quartz jobs were scheduled after the scheduler was started which resulted in a race +condition where duplicate `TRIGGER_ACCESS` entries were being added to the `QRTZ_LOCKS` table. +This has been fixed." diff --git a/hapi-fhir-jpa/src/main/java/ca/uhn/fhir/jpa/sched/BaseSchedulerServiceImpl.java b/hapi-fhir-jpa/src/main/java/ca/uhn/fhir/jpa/sched/BaseSchedulerServiceImpl.java index bed64818402..4a9375e01d0 100644 --- a/hapi-fhir-jpa/src/main/java/ca/uhn/fhir/jpa/sched/BaseSchedulerServiceImpl.java +++ b/hapi-fhir-jpa/src/main/java/ca/uhn/fhir/jpa/sched/BaseSchedulerServiceImpl.java @@ -145,6 +145,14 @@ public abstract class BaseSchedulerServiceImpl implements ISchedulerService { @EventListener(ContextRefreshedEvent.class) public void start() { + + // Jobs are scheduled first to avoid a race condition that occurs if jobs are scheduled + // after the scheduler starts for the first time. This race condition results in duplicate + // TRIGGER_ACCESS entries being added to the QRTZ_LOCKS table. + // Note - Scheduling jobs before the scheduler has started is supported by Quartz + // http://www.quartz-scheduler.org/documentation/quartz-2.3.0/cookbook/CreateScheduler.html + scheduleJobs(); + myStopping.set(false); try { @@ -159,8 +167,6 @@ public abstract class BaseSchedulerServiceImpl implements ISchedulerService { ourLog.error("Failed to start scheduler", e); throw new ConfigurationException(Msg.code(1632) + "Failed to start scheduler", e); } - - scheduleJobs(); } private void scheduleJobs() {