This issue is presumed to occur 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, but we confirmed that the DataSource in LocalDataSourceJobStore
and the Autowired DataSource are mismatched.
This issue may not always reproduce on the first full test run.
- Run the ALL test suite:
./gradlew test
- If all tests pass initially: Run multiple times or test on different environments
- Why this happens: The issue depends on Spring Test Context's DataSource initialization timing and ApplicationContext lifecycle
Successful Test (No Issues):
Provider DataSource == Autowired DataSource: true
Failing Test (Connection Closed Error):
Provider DataSource == Autowired DataSource: false
- First Test: LocalDataSourceJobStore initializes with DataSource A
- Second Test: Spring creates new ApplicationContext with DataSource B
- Problem: LocalDataSourceJobStore still references DataSource A (Closed)
- Result: "Connection is closed" error when Quartz tries to use DataSource A
QuartzConnectionIssueTest
: Basic test (may succeed due to context reuse)QuartzConnectionIssueWithMockTest
: Most reliable reproduction (forces new ApplicationContext)QuartzConnectionClosedTest
: Additional test to induce Connection Closed issues (ApplicationContext recreation)
QuartzConnectionFixedTest
: Demonstrates the fix using TestExecutionListener
java.sql.SQLException: Connection is closed
at org.quartz.impl.jdbcjobstore.LockException:
Failure obtaining db row lock: Connection is closed
@ExtendWith(QuartzConnectionResetListener.class)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
- Spring Boot 3.5.3
- Quartz Scheduler
- H2 In-Memory Database
- JUnit 5