use of org.adbcj.support.DefaultDbFuture in project adbcj by mheath.
the class JdbcConnectionManager method close.
public DbFuture<Void> close(boolean immediate) throws DbException {
synchronized (lock) {
if (closeFuture == null) {
closeFuture = new DefaultDbFuture<Void>();
closeFuture.addListener(new DbListener<Void>() {
@Override
public void onCompletion(DbFuture<Void> future) throws Exception {
executorService.shutdown();
}
});
} else {
return closeFuture;
}
}
final AtomicInteger countDown = new AtomicInteger();
final AtomicBoolean allClosed = new AtomicBoolean(false);
DbListener<Void> listener = new DbListener<Void>() {
@Override
public void onCompletion(DbFuture<Void> future) {
try {
int count = countDown.decrementAndGet();
future.get();
if (allClosed.get() && count == 0) {
closeFuture.setResult(null);
}
} catch (Exception e) {
// If the connection close errored out, error out our closeFuture too
closeFuture.setException(e);
}
}
};
synchronized (lock) {
for (JdbcConnection connection : connections) {
countDown.incrementAndGet();
connection.close(immediate).addListener(listener);
}
}
allClosed.set(true);
if (countDown.get() == 0) {
closeFuture.setResult(null);
}
return closeFuture;
}
Aggregations