Package org.adbcj

Examples of org.adbcj.Connection


      connection.close(true);
    }
  }

  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);
    }
  }
View Full Code Here


      connection.close(true);
    }
  }

  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);
    }
  }
View Full Code Here

    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");
  }
View Full Code Here

  @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 {
View Full Code Here

  public static void main(String[] args) throws Exception {
    ConnectionManager mysqlCM = ConnectionManagerProvider.createConnectionManager("adbcj:mysqlnetty://localhost/adbcjtck", "adbcjtck", "adbcjtck");
    ConnectionManager pgCM = ConnectionManagerProvider.createConnectionManager("adbcj:postgresql-netty://localhost/adbcjtck", "adbcjtck", "adbcjtck");

    Connection mysql = mysqlCM.connect().getUninterruptably();
    Connection pg = pgCM.connect().getUninterruptably();

    final String insertTemplate = "INSERT INTO large (a, b, c) VALUES ('%s', '%s', '%s')";
    for (int i = 0; i < 998; i++) {
      String a = randString();
      String b = randString();
      String c = randString();
      final String insert = String.format(insertTemplate, a, b, c);
      mysql.executeUpdate(insert).get();
      pg.executeUpdate(insert).get();
    }
//    mysql.close(false).get();
//    pg.close(false).get();
//    mysqlCM.close(true);
//    pgCM.close(true);
View Full Code Here

        // Indicate that callback has been invoked
        callbacks[0] = true;
        latch.countDown();
      }
    });
    Connection connection = connectFuture.get(5, TimeUnit.SECONDS);
    assertTrue(!connection.isClosed());
    DbFuture<Void> closeFuture = connection.close(true).addListener(new DbListener<Void>() {
      public void onCompletion(DbFuture<Void> future) throws Exception {
        // Indicate that callback has been invoked
        callbacks[1] = true;
        latch.countDown();
      }
    });
    closeFuture.get(5, TimeUnit.SECONDS);
    assertTrue(connection.isClosed());
    latch.await(1, TimeUnit.SECONDS);
    assertTrue(callbacks[0], "Callback on connection future was not invoked");
    assertTrue(callbacks[1], "Callback on close future was not invoked");

    connectionManager.close(true);
View Full Code Here

        // Indicate that callback has been invoked
        callbacks[0] = true;
        latch.countDown();
      }
    });
    Connection connection = connectFuture.get(5, TimeUnit.SECONDS);
    assertTrue(!connection.isClosed());
    DbFuture<Void> closeFuture = connection.close(true).addListener(new DbListener<Void>() {
      public void onCompletion(DbFuture<Void> future) throws Exception {
        // Indicate that callback has been invoked
        callbacks[1] = true;
        latch.countDown();
      }
    });
    closeFuture.get(5, TimeUnit.SECONDS);
    assertTrue(connection.isClosed());
    latch.await(1, TimeUnit.SECONDS);
    assertTrue(callbacks[0], "Callback on connection future was not invoked");
    assertTrue(callbacks[1], "Callback on finalizeClose future was not invoked");
  }
View Full Code Here

  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");
  }
View Full Code Here

  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");
  }
View Full Code Here

    assertTrue(closeCallback[0], "The finalizeClose callback was not invoked when cancelled");
    assertTrue(closeCallback[1], "The finalizeClose future did not indicate the finalizeClose was cancelled");
  }
 
  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()) {
          future.get(); // Will throw exception if failed
        } 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");
    }
  }
View Full Code Here

TOP

Related Classes of org.adbcj.Connection

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.