Search in sources :

Example 1 with DbListener

use of org.adbcj.DbListener in project adbcj by mheath.

the class ConnectTest method testConnectNonImmediateClose.

public void testConnectNonImmediateClose() throws DbException, InterruptedException {
    final boolean[] callbacks = { false };
    final CountDownLatch latch = new CountDownLatch(1);
    Connection connection = connectionManager.connect().get();
    assertTrue(!connection.isClosed());
    connection.close(true).addListener(new DbListener<Void>() {

        public void onCompletion(DbFuture<Void> future) throws Exception {
            // Indicate that finalizeClose callback has been invoked
            callbacks[0] = true;
            latch.countDown();
        }
    }).get();
    assertTrue(connection.isClosed());
    latch.await(1, TimeUnit.SECONDS);
    assertTrue(callbacks[0], "Callback on finalizeClose future was not invoked");
}
Also used : Connection(org.adbcj.Connection) DbListener(org.adbcj.DbListener) CountDownLatch(java.util.concurrent.CountDownLatch) DbFuture(org.adbcj.DbFuture)

Example 2 with DbListener

use of org.adbcj.DbListener 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)

Example 3 with DbListener

use of org.adbcj.DbListener in project adbcj by mheath.

the class SelectForUpdateTest method testSelectForUpdate.

public void testSelectForUpdate() throws Exception {
    logger.debug("Using connection manager: {}", connectionManager);
    final boolean[] invoked = { false, false };
    final AtomicBoolean locked = new AtomicBoolean(false);
    final AtomicBoolean error = new AtomicBoolean(false);
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    Connection conn1 = connectionManager.connect().get();
    Connection conn2 = connectionManager.connect().get();
    logger.debug("Obtained connection managers");
    // Get lock on locks table
    conn1.beginTransaction();
    TestUtils.selectForUpdate(conn1, new DbListener<ResultSet>() {

        public void onCompletion(DbFuture<ResultSet> future) throws Exception {
            logger.debug("In first callback");
            locked.set(true);
            invoked[0] = true;
            latch1.countDown();
        }
    }).get();
    logger.debug("Obtained lock on locks table");
    // Try to get lock with second connection
    conn2.beginTransaction();
    DbFuture<ResultSet> future = TestUtils.selectForUpdate(conn2, new DbListener<ResultSet>() {

        public void onCompletion(DbFuture<ResultSet> future) throws Exception {
            logger.debug("In second callback");
            invoked[1] = true;
            if (!locked.get()) {
                error.set(true);
            }
            latch2.countDown();
        }
    });
    logger.debug("Select for update called with second connection, should be blocking");
    assertTrue(latch1.await(1, TimeUnit.SECONDS));
    assertTrue(invoked[0], "First SELECT FOR UPDATE callback should have been invoked");
    assertTrue(locked.get(), "locked should be set");
    assertFalse(invoked[1], "Second SELCT FOR UPDATE callback should not have been invoked yet");
    assertFalse(error.get());
    conn1.rollback().get();
    logger.debug("Released first lock");
    future.get();
    logger.debug("Second SELECT FOR UPDATE completed");
    assertTrue(latch2.await(1, TimeUnit.SECONDS));
    assertTrue(invoked[1]);
    assertFalse(error.get(), "An error occurred during SELECT FOR UPDATE");
    conn2.rollback().get();
    logger.debug("Released second lock");
    // Close connections
    logger.debug("Closing connections");
    conn1.close(true).get();
    logger.debug("Closed connection 1");
    conn2.close(true).get();
    logger.debug("Closed connection 2");
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) DbListener(org.adbcj.DbListener) CountDownLatch(java.util.concurrent.CountDownLatch) DbFuture(org.adbcj.DbFuture)

Example 4 with DbListener

use of org.adbcj.DbListener in project adbcj by mheath.

the class SelectTest method testSimpleSelect.

public void testSimpleSelect() throws DbException, InterruptedException {
    final boolean[] callbacks = { false };
    final CountDownLatch latch = new CountDownLatch(callbacks.length);
    Connection connection = connectionManager.connect().get();
    try {
        ResultSet resultSet = connection.executeQuery("SELECT int_val, str_val FROM simple_values ORDER BY int_val").addListener(new DbListener<ResultSet>() {

            public void onCompletion(DbFuture<ResultSet> future) throws Exception {
                System.out.println("In callback");
                future.get().size();
                callbacks[0] = true;
                latch.countDown();
                System.out.println("Finished callback");
            }
        }).get();
        Assert.assertEquals(6, resultSet.size());
        Iterator<Row> i = resultSet.iterator();
        Row nullRow = null;
        Row row = i.next();
        if (row.get(0).isNull()) {
            nullRow = row;
            row = i.next();
        }
        Assert.assertEquals(row.get(0).getInt(), 0);
        Assert.assertEquals(row.get(1).getValue(), "Zero");
        row = i.next();
        Assert.assertEquals(row.get(0).getInt(), 1);
        Assert.assertEquals(row.get(1).getValue(), "One");
        row = i.next();
        Assert.assertEquals(row.get(0).getInt(), 2);
        Assert.assertEquals(row.get(1).getValue(), "Two");
        row = i.next();
        Assert.assertEquals(row.get(0).getInt(), 3);
        Assert.assertEquals(row.get(1).getValue(), "Three");
        row = i.next();
        Assert.assertEquals(row.get(0).getInt(), 4);
        Assert.assertEquals(row.get(1).getValue(), "Four");
        if (i.hasNext() && nullRow == null) {
            nullRow = i.next();
        }
        Assert.assertEquals(nullRow.get(0).getValue(), null);
        Assert.assertEquals(nullRow.get(1).getValue(), null);
        Assert.assertTrue(!i.hasNext(), "There were too many rows in result set");
        latch.await();
        Assert.assertTrue(callbacks[0], "Result set callback was not invoked");
    } finally {
        connection.close(true);
    }
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) DbListener(org.adbcj.DbListener) Row(org.adbcj.Row) CountDownLatch(java.util.concurrent.CountDownLatch) DbFuture(org.adbcj.DbFuture)

Aggregations

DbFuture (org.adbcj.DbFuture)4 DbListener (org.adbcj.DbListener)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Connection (org.adbcj.Connection)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ResultSet (org.adbcj.ResultSet)2 SQLException (java.sql.SQLException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 DbException (org.adbcj.DbException)1 Row (org.adbcj.Row)1 DefaultDbFuture (org.adbcj.support.DefaultDbFuture)1