Calling interrupt() on thread that access the database closes db connection and causes ConnectionClosed exceptions (H2 and Derby)

Description

Using H2 or Derby in embedded mode leads to lots of ConnectionClosed exceptions in the log during a call to JobAcquisitionThread#shutdown. Why? When the database sees the interrupted flag set, it automatically closes the connection (at least H2 and Derby behave like this). The JobAcquisitionThread then trying to get the jobs from the db throws connection closed exceptions (AcquireJobsCmd).

Suggestion: use wait/notify or similar classes from the java.util.concurrent package to wake up the sleeping thread (JobAcquisitionThread:102) rather than interrupting the thread since this has the side-effect on the database as mentioned above.

See also:
http://db.apache.org/derby/docs/10.8/devguide/cdevdvlp22619.html

Code that causes problems:

public void jobWasAdded() {
this.isJobAdded = true;
LOGGER.fine("Job was added. Interrupting " + this);
interrupt(); // !!!
}

public void shutdown() {
if (this.isActive) {
LOGGER.info(getName() + " is shutting down");
this.isActive = false;
interrupt(); // !!!
try {
join();
} catch (InterruptedException e) {
LOGGER.log(Level.WARNING, "Interruption while shutting down " + this.getClass().getName(), e);
}
}
}

Environment

None

Assignee

Daniel Meyer (camunda)

Reporter

Etienne Studer

Labels

None

Fix versions

Affects versions

Priority

Critical
Configure