Search in sources :

Example 1 with ResultSet

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

the class ConnectTest method testNonImmediateClose.

public void testNonImmediateClose() throws Exception {
    Connection connection = connectionManager.connect().get();
    List<DbSessionFuture<ResultSet>> futures = new ArrayList<DbSessionFuture<ResultSet>>();
    for (int i = 0; i < 5; i++) {
        futures.add(connection.executeQuery(String.format("SELECT *, %d FROM simple_values", i)));
    }
    try {
        connection.close(false).get(10, TimeUnit.SECONDS);
    } catch (TimeoutException e) {
        for (DbSessionFuture<ResultSet> future : futures) {
            if (future.isDone()) {
                // Will throw exception if failed
                future.get();
            } else {
                throw new AssertionError("future " + future + " did not complete in time");
            }
        }
        throw new AssertionError("finalizeClose future failed to complete");
    }
    assertTrue(connection.isClosed(), "Connection should be closed");
    for (DbSessionFuture<ResultSet> future : futures) {
        assertTrue(future.isDone(), "Request did not finish before connection was closed: " + future);
        assertFalse(future.isCancelled(), "Future was cancelled and should have been");
    }
}
Also used : Connection(org.adbcj.Connection) ArrayList(java.util.ArrayList) ResultSet(org.adbcj.ResultSet) DbSessionFuture(org.adbcj.DbSessionFuture) TimeoutException(java.util.concurrent.TimeoutException)

Example 2 with ResultSet

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

the class SelectTest method testMultipleSelectStatements.

public void testMultipleSelectStatements() throws Exception {
    Connection connection = connectionManager.connect().get();
    List<DbFuture<ResultSet>> futures = new LinkedList<DbFuture<ResultSet>>();
    for (int i = 0; i < 1000; i++) {
        futures.add(connection.executeQuery(String.format("SELECT *, %d FROM simple_values", i)));
    }
    for (DbFuture<ResultSet> future : futures) {
        try {
            future.get(5, TimeUnit.MINUTES);
        } catch (TimeoutException e) {
            throw new AssertionError("Timed out waiting on future: " + future);
        }
    }
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) DbFuture(org.adbcj.DbFuture) LinkedList(java.util.LinkedList) TimeoutException(java.util.concurrent.TimeoutException)

Example 3 with ResultSet

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

the class TransactionTest method testCommit.

public void testCommit() throws Exception {
    Connection connection = connectionManager.connect().get();
    Connection connection2 = connectionManager.connect().get();
    try {
        // Clear out updates table
        Result result = connection.executeUpdate("DELETE FROM updates").get();
        assertNotNull(result);
        connection.beginTransaction();
        // Insert a row
        result = connection.executeUpdate("INSERT INTO updates (id) VALUES (1)").get();
        assertNotNull(result);
        assertEquals(result.getAffectedRows(), Long.valueOf(1));
        // Make sure second connection can't see data
        ResultSet rs = connection2.executeQuery("SELECT id FROM updates").get();
        assertNotNull(rs);
        assertEquals(rs.size(), 0);
        connection.commit().get();
        // Make sure both connections can see data
        rs = connection.executeQuery("SELECT id FROM updates").get();
        assertNotNull(rs);
        assertEquals(rs.size(), 1);
        assertEquals(rs.get(0).get(0).getInt(), 1);
        rs = connection2.executeQuery("SELECT id FROM updates").get();
        assertNotNull(rs);
        assertEquals(rs.size(), 1);
        assertEquals(rs.get(0).get(0).getInt(), 1);
    } finally {
        connection.close(true);
        connection2.close(true);
    }
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) Result(org.adbcj.Result)

Example 4 with ResultSet

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

the class TransactionTest method testRollback.

public void testRollback() throws Exception {
    Connection connection = connectionManager.connect().get();
    try {
        // Clear out updates table
        Result result = connection.executeUpdate("DELETE FROM updates").get();
        assertNotNull(result);
        // Make sure updates is empty
        ResultSet rs = connection.executeQuery("SELECT id FROM updates").get();
        assertNotNull(rs);
        assertEquals(rs.size(), 0);
        connection.beginTransaction();
        // Insert a row
        result = connection.executeUpdate("INSERT INTO updates (id) VALUES (1)").get();
        assertNotNull(result);
        assertEquals(result.getAffectedRows(), Long.valueOf(1));
        // Make sure we can select the row
        rs = connection.executeQuery("SELECT id FROM updates").get();
        assertNotNull(rs);
        assertEquals(rs.size(), 1);
        Value value = rs.get(0).get(0);
        assertEquals(value.getInt(), 1);
        // Rollback transaction
        connection.rollback().get();
        // select query should now be empty
        rs = connection.executeQuery("SELECT id FROM updates").get();
        assertNotNull(rs);
        assertEquals(rs.size(), 0);
    } finally {
        connection.close(true);
    }
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) Value(org.adbcj.Value) Result(org.adbcj.Result)

Example 5 with ResultSet

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

the class UpdateTest method testSimpleUpdates.

public void testSimpleUpdates() throws InterruptedException {
    Connection connection = connectionManager.connect().get();
    assertNotNull(connection);
    // Clear out updates table
    Result result = connection.executeUpdate("DELETE FROM updates").get();
    assertNotNull(result);
    // Insert a row
    result = connection.executeUpdate("INSERT INTO updates (id) VALUES (1)").get();
    assertNotNull(result);
    assertEquals(result.getAffectedRows(), Long.valueOf(1));
    // Select the row
    ResultSet rs = connection.executeQuery("SELECT id FROM updates").get();
    assertNotNull(rs);
    assertEquals(rs.size(), 1);
    Value value = rs.get(0).get(0);
    assertEquals(value.getInt(), 1);
    assertEquals(value.getField().getColumnLabel(), "id");
    // Update nothing
    result = connection.executeUpdate("UPDATE updates SET id=1 WHERE id=2").get();
    assertNotNull(result);
    assertEquals(result.getAffectedRows(), Long.valueOf(0));
    // Update inserted row
    result = connection.executeUpdate("UPDATE updates SET id=2").get();
    assertNotNull(result);
    assertEquals(result.getAffectedRows(), Long.valueOf(1));
    // Delete inserted row
    result = connection.executeUpdate("DELETE FROM updates WHERE id=2").get();
    assertNotNull(result);
    assertEquals(result.getAffectedRows(), Long.valueOf(1));
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) Value(org.adbcj.Value) Result(org.adbcj.Result)

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