Search in sources :

Example 6 with ResultSet

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

the class PgTest method main.

/**
	 * @param args
	 * @throws InterruptedException
	 * @throws org.adbcj.DbException
	 */
public static void main(String[] args) throws DbException, InterruptedException {
    ConnectionManager cm = ConnectionManagerProvider.createConnectionManager("adbcj:postgresql-netty://localhost/adbcjtck", "adbcjtck", "adbcjtck");
    Connection connection = cm.connect().get();
    final DbFuture<ResultSet> future = connection.executeQuery("SELECT * FROM simple_values");
    final DbFuture<ResultSet> future2 = connection.executeQuery("SELECT * FROM large");
    ResultSet rs = future.get();
    ResultSet rs2 = future2.get();
    for (Row row : rs) {
        System.out.println(row.get(0) + " " + row.get(1));
    }
    for (Row row : rs2) {
        System.out.println(row.get(0) + " " + row.get(1));
    }
    connection.close(true).get();
    cm.close(true).get();
    System.out.println("Closed");
}
Also used : ConnectionManager(org.adbcj.ConnectionManager) Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) Row(org.adbcj.Row)

Example 7 with ResultSet

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

the class ProtocolHandler method handleResultSetRowResponse.

private void handleResultSetRowResponse(AbstractMySqlConnection connection, ResultSetRowResponse message) {
    Request<ResultSet> activeRequest = connection.getActiveRequest();
    ResultSetRowResponse rowResponse = (ResultSetRowResponse) message;
    activeRequest.getEventHandler().startRow(activeRequest.getAccumulator());
    for (Value value : rowResponse.getValues()) {
        activeRequest.getEventHandler().value(value, activeRequest.getAccumulator());
    }
    activeRequest.getEventHandler().endRow(activeRequest.getAccumulator());
}
Also used : ResultSet(org.adbcj.ResultSet) Value(org.adbcj.Value)

Example 8 with ResultSet

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

the class ConnectSpecialCaseTest method testImmediateClose.

//	@Parameters({"url", "user", "password"})
//	@Test(timeOut=60000)
//	public void testConnectCancel(String url, String user, String password) throws Exception {
//		StringBuilder urlBuilder = new StringBuilder();
//
//		URI connectUrl = new URI(url);
//		String scheme = connectUrl.getScheme();
//		while (scheme != null) {
//			urlBuilder.append(scheme).append(":");
//			connectUrl = new URI(connectUrl.getSchemeSpecificPart());
//			scheme = connectUrl.getScheme();
//		}
//
//		urlBuilder.append("//").append(UNREACHABLE_HOST);
//		urlBuilder.append(connectUrl.getPath());
//
//		final boolean[] callbacks = {false};
//		final CountDownLatch latch = new CountDownLatch(1);
//
//		ConnectionManager connectionManager = ConnectionManagerProvider.createConnectionManager(urlBuilder.toString(), "dummyuser", "dummypassword");
//		try {
//			DbFuture<Connection> connectFuture = connectionManager.connect().addListener(new DbListener<Connection>() {
//				public void onCompletion(DbFuture<Connection> future) throws Exception {
//					callbacks[0] = true;
//					latch.countDown();
//				}
//			});
//			assertTrue(connectFuture.cancel(true), "Connection to unreachable host was not canceled");
//			assertTrue(connectFuture.isCancelled());
//			assertTrue(latch.await(1, TimeUnit.SECONDS), "Callback was not invoked in time");
//			assertTrue(callbacks[0], "Connect future callback was not invoked with connect cancellation");
//		} finally {
//			connectionManager.finalizeClose(true);
//		}
//	}
@Parameters({ "url", "user", "password" })
@Test(timeOut = 60000)
public void testImmediateClose(String url, String user, String password) throws InterruptedException {
    ConnectionManager connectionManager = ConnectionManagerProvider.createConnectionManager(url, user, password);
    try {
        Connection lockingConnection = connectionManager.connect().get();
        connectionManager.setPipeliningEnabled(false);
        Connection connection = connectionManager.connect().get();
        lockingConnection.beginTransaction();
        TestUtils.selectForUpdate(lockingConnection).get();
        List<DbSessionFuture<ResultSet>> futures = new ArrayList<DbSessionFuture<ResultSet>>();
        connection.beginTransaction();
        TestUtils.selectForUpdate(connection);
        for (int i = 0; i < 5; i++) {
            futures.add(connection.executeQuery(String.format("SELECT *, %d FROM simple_values", i)));
        }
        logger.debug("Closing connection");
        connection.close(true).get();
        logger.debug("Closed");
        logger.debug("Closing locking connection");
        lockingConnection.rollback().get();
        lockingConnection.close(true).get();
        logger.debug("Locking connection finalizeClose");
        assertTrue(connection.isClosed(), "Connection should be closed");
        for (DbSessionFuture<ResultSet> future : futures) {
            assertTrue(future.isCancelled(), "Future should have been cancelled at finalizeClose: " + future);
            assertTrue(future.isDone(), "Request did not finish before connection was closed: " + future);
        }
    } finally {
        connectionManager.close(true).get();
    }
}
Also used : ConnectionManager(org.adbcj.ConnectionManager) Connection(org.adbcj.Connection) ArrayList(java.util.ArrayList) ResultSet(org.adbcj.ResultSet) DbSessionFuture(org.adbcj.DbSessionFuture) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test)

Example 9 with ResultSet

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

the class ConnectTest method testCancelClose.

public void testCancelClose() throws DbException, InterruptedException {
    final boolean[] closeCallback = { false, false };
    // This connection is used for doing a select for update lock
    Connection lockConnection = connectionManager.connect().get();
    Connection connectionToClose = connectionManager.connect().get();
    try {
        // Get lock with select for update
        lockConnection.beginTransaction();
        TestUtils.selectForUpdate(lockConnection).get();
        // Do select for update on second connection so we can finalizeClose it and then cancel the finalizeClose
        connectionToClose.beginTransaction();
        DbFuture<ResultSet> future = TestUtils.selectForUpdate(connectionToClose);
        DbSessionFuture<Void> closeFuture = connectionToClose.close(false).addListener(new DbListener<Void>() {

            public void onCompletion(DbFuture<Void> future) throws Exception {
                logger.debug("testCancelClose: In finalizeClose callback for connectionManager {}", connectionManager);
                closeCallback[0] = true;
                closeCallback[1] = future.isCancelled();
            }
        });
        assertTrue(connectionToClose.isClosed(), "This connection should be flagged as closed now");
        assertTrue(closeFuture.cancel(false), "The connection finalizeClose should have cancelled properly");
        assertFalse(connectionToClose.isClosed(), "This connection should not be closed because we canceled the finalizeClose");
        // Release lock
        lockConnection.rollback().get();
        // Make sure closingConnection's select for update completed successfully
        future.get();
        connectionToClose.rollback().get();
    } finally {
        if (lockConnection.isInTransaction()) {
            lockConnection.rollback().get();
        }
        if (connectionToClose.isInTransaction()) {
            connectionToClose.rollback().get();
        }
        lockConnection.close(true);
        connectionToClose.close(true);
    }
    // Make sure the finalizeClose's callback was invoked properly
    assertTrue(closeCallback[0], "The finalizeClose callback was not invoked when cancelled");
    assertTrue(closeCallback[1], "The finalizeClose future did not indicate the finalizeClose was cancelled");
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) TimeoutException(java.util.concurrent.TimeoutException) DbException(org.adbcj.DbException)

Example 10 with ResultSet

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

Aggregations

ResultSet (org.adbcj.ResultSet)12 Connection (org.adbcj.Connection)11 TimeoutException (java.util.concurrent.TimeoutException)3 DbFuture (org.adbcj.DbFuture)3 Result (org.adbcj.Result)3 Value (org.adbcj.Value)3 ArrayList (java.util.ArrayList)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ConnectionManager (org.adbcj.ConnectionManager)2 DbException (org.adbcj.DbException)2 DbListener (org.adbcj.DbListener)2 DbSessionFuture (org.adbcj.DbSessionFuture)2 Row (org.adbcj.Row)2 LinkedList (java.util.LinkedList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Parameters (org.testng.annotations.Parameters)1 Test (org.testng.annotations.Test)1