Search in sources :

Example 6 with Connection

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

the class TransactionTest method testCommitRollbackWithNoTransaction.

public void testCommitRollbackWithNoTransaction() throws Exception {
    Connection connection = connectionManager.connect().get();
    try {
        // Test commit with no transaction
        try {
            connection.commit();
            Assert.fail("Not in transaction, commit should have failed");
        } catch (DbException e) {
        // Pass
        }
        // Test rollback with no transaction
        try {
            connection.rollback();
            Assert.fail("Not in transaction, rollback should have failed");
        } catch (DbException e) {
        // Pass
        }
        connection.beginTransaction();
        connection.rollback().get();
        connection.beginTransaction();
        connection.commit().get();
        connection.beginTransaction();
    } finally {
        connection.close(true);
    }
}
Also used : Connection(org.adbcj.Connection) DbException(org.adbcj.DbException)

Example 7 with Connection

use of org.adbcj.Connection 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 8 with Connection

use of org.adbcj.Connection 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 9 with Connection

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

the class TransactionTest method testBeginTransaction.

public void testBeginTransaction() throws Exception {
    Connection connection = connectionManager.connect().get();
    try {
        Assert.assertTrue(!connection.isInTransaction(), "Connections should not start with transaction started");
        connection.beginTransaction();
        Assert.assertTrue(connection.isInTransaction(), "Connection should be in transaction");
        try {
            connection.beginTransaction();
            Assert.fail("Should have thrown exception because connection is already in transaction");
        } catch (DbException e) {
        // Pass
        }
    } finally {
        connection.close(true);
    }
}
Also used : Connection(org.adbcj.Connection) DbException(org.adbcj.DbException)

Example 10 with Connection

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

Connection (org.adbcj.Connection)21 ResultSet (org.adbcj.ResultSet)11 DbException (org.adbcj.DbException)8 CountDownLatch (java.util.concurrent.CountDownLatch)5 ConnectionManager (org.adbcj.ConnectionManager)5 TimeoutException (java.util.concurrent.TimeoutException)4 DbFuture (org.adbcj.DbFuture)4 Result (org.adbcj.Result)4 DbListener (org.adbcj.DbListener)3 ArrayList (java.util.ArrayList)2 DbSessionFuture (org.adbcj.DbSessionFuture)2 Row (org.adbcj.Row)2 Value (org.adbcj.Value)2 Parameters (org.testng.annotations.Parameters)2 Test (org.testng.annotations.Test)2 SQLException (java.sql.SQLException)1 LinkedList (java.util.LinkedList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 DbFutureConcurrentProxy (org.adbcj.support.DbFutureConcurrentProxy)1 DefaultResult (org.adbcj.support.DefaultResult)1