Image

Problem

When Spring Test Context recreates ApplicationContext, the DataSource field in LocalDataSourceJobStore is not updated, causing "Connection is closed" errors.

Confirmed issue: The DataSource in LocalDataSourceJobStore and the Autowired DataSource are mismatched when ApplicationContext is recreated.

This issue has been reported by others as well: A problem with Quartz and Spring Test Context caching


Reproduce issue

Reproduce using this demo repository.

Repository: https://github.com/PhysicksKim/quartz-spring-connection-issue

Note: Run full test suite multiple times - this issue may not always reproduce on first run.


Expected Error

java.sql.SQLException: Connection is closed
    at org.quartz.impl.jdbcjobstore.LockException:
    Failure obtaining db row lock: Connection is closed

Evidence

Test logs show the mismatch:

// Passed test
Provider DataSource == Autowired DataSource: true
// Failed test
Provider DataSource == Autowired DataSource: false

Why this issue happened

This issue occurs when Spring Test Context recreates ApplicationContext but does not update the DataSource field in LocalDataSourceJobStore.

When DataSource and DB Connection are newly created, the DataSource used by Quartz for Job access should also be updated. However, I confirmed that the DataSource in LocalDataSourceJobStore and the Autowired DataSource become mismatched.

Proposed Solution

LocalDataSourceJobStore should detect DataSource changes and update its DataSource field when Spring Test Context recreates ApplicationContext.


Tested On

  • macOS (MacBook Air M1 2020)
  • Windows 10 (Intel i5-8400)