what can I do to make sure that never happens?
One thing to consider is to switch to using an Executors.newSingleThreadExecutor()
which will only fork one thread to do the running. Then you can submit as many tasks as you want without having to worry about them overlapping.
private final ExecutorService threadPool =
Executors.newSingleThreadExecutor(/* pass in ThreadFactory to set name */);
...
DBFailoverRecoveryService recoveryService =
new DBFailoverRecoveryService(this.dbFailoverMutex, this.dataSource);
threadPool.submit(recoveryService);
As always with the ExecutorService
, you need to call threadPool.shutdown()
once you submit the last task to the pool otherwise it will hang your application. You could add a Datasource.destroy();
method to do this.