Search in sources :

Example 1 with Result

use of org.adbcj.Result 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 2 with Result

use of org.adbcj.Result 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 3 with Result

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

Example 4 with Result

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

the class ProtocolHandler method handleOkResponse.

private void handleOkResponse(AbstractMySqlConnection connection, OkResponse response) {
    logger.trace("Response '{}' on connection {}", response, connection);
    List<String> warnings = null;
    if (response.getWarningCount() > 0) {
        warnings = new LinkedList<String>();
        for (int i = 0; i < response.getWarningCount(); i++) {
            warnings.add(response.getMessage());
        }
    }
    logger.warn("Warnings: {}", warnings);
    Request<Result> activeRequest = connection.getActiveRequest();
    if (activeRequest == null) {
        // TODO Do we need to pass the warnings on to the connection?
        DefaultDbFuture<Connection> connectFuture = connection.getConnectFuture();
        if (!connectFuture.isDone()) {
            connectFuture.setResult(connection);
            return;
        } else {
            throw new IllegalStateException("Received an OkResponse with no activeRequest " + response);
        }
    }
    Result result = new DefaultResult(response.getAffectedRows(), warnings);
    activeRequest.complete(result);
}
Also used : Connection(org.adbcj.Connection) DefaultResult(org.adbcj.support.DefaultResult) DefaultResult(org.adbcj.support.DefaultResult) Result(org.adbcj.Result)

Aggregations

Connection (org.adbcj.Connection)4 Result (org.adbcj.Result)4 ResultSet (org.adbcj.ResultSet)3 Value (org.adbcj.Value)2 DefaultResult (org.adbcj.support.DefaultResult)1