Search in sources :

Example 1 with DefaultDbFuture

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;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DbListener(org.adbcj.DbListener) DefaultDbFuture(org.adbcj.support.DefaultDbFuture) DbFuture(org.adbcj.DbFuture) DbException(org.adbcj.DbException) SQLException(java.sql.SQLException)

Aggregations

SQLException (java.sql.SQLException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DbException (org.adbcj.DbException)1 DbFuture (org.adbcj.DbFuture)1 DbListener (org.adbcj.DbListener)1 DefaultDbFuture (org.adbcj.support.DefaultDbFuture)1