Package org.adbcj

Examples of org.adbcj.Connection


public class Test {

  public static void main(String[] args) throws DbException, Exception {
    ConnectionManager cm = ConnectionManagerProvider.createConnectionManager("adbcj:mysqlnetty://localhost/test", "foo", "dawg");
    Connection connection = cm.connect().get();
    connection.close(true);
  }
View Full Code Here


  public void testSimpleSelect() throws DbException, InterruptedException {
    final boolean[] callbacks = {false};
    final CountDownLatch latch = new CountDownLatch(callbacks.length);
   
    Connection connection = connectionManager.connect().get();
    try {
      ResultSet resultSet = connection.executeQuery("SELECT int_val, str_val FROM simple_values ORDER BY int_val").addListener(new DbListener<ResultSet>() {
        public void onCompletion(DbFuture<ResultSet> future) throws Exception {
          System.out.println("In callback");
          future.get().size();
          callbacks[0] = true;
          latch.countDown();
          System.out.println("Finished callback");
        }
      }).get();
     
      Assert.assertEquals(6, resultSet.size());
     
      Iterator<Row> i = resultSet.iterator();
     
      Row nullRow = null;
      Row row = i.next();
      if (row.get(0).isNull()) {
        nullRow = row;
        row = i.next();
      }
      Assert.assertEquals(row.get(0).getInt(), 0);
      Assert.assertEquals(row.get(1).getValue(), "Zero");
      row = i.next();
      Assert.assertEquals(row.get(0).getInt(), 1);
      Assert.assertEquals(row.get(1).getValue(), "One");
      row = i.next();
      Assert.assertEquals(row.get(0).getInt(), 2);
      Assert.assertEquals(row.get(1).getValue(), "Two");
      row = i.next();
      Assert.assertEquals(row.get(0).getInt(), 3);
      Assert.assertEquals(row.get(1).getValue(), "Three");
      row = i.next();
      Assert.assertEquals(row.get(0).getInt(), 4);
      Assert.assertEquals(row.get(1).getValue(), "Four");
 
      if (i.hasNext() && nullRow == null) {
        nullRow = i.next();
      }
     
      Assert.assertEquals(nullRow.get(0).getValue(), null);
      Assert.assertEquals(nullRow.get(1).getValue(), null);
 
     
      Assert.assertTrue(!i.hasNext(), "There were too many rows in result set");
     
      latch.await();
      Assert.assertTrue(callbacks[0], "Result set callback was not invoked");
    } finally {
      connection.close(true);
    }
  }
View Full Code Here

      connection.close(true);
    }
  }
 
  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 {
View Full Code Here

      }
    }
  }
 
  public void testBrokenSelect() throws Exception {
    Connection connection = connectionManager.connect().get();
   
    DbSessionFuture<ResultSet> future = connection.executeQuery("SELECT broken_query");
    try {
      future.get(5, TimeUnit.SECONDS);
      throw new AssertionError("Issues a bad query, future should have failed");
    } catch (DbException e) {
      // Pass
    } finally {
      connection.close(true).get();
    }
  }
View Full Code Here

    DbFuture<Void> closeFuture = connectionManager.close(true);
    closeFuture.getUninterruptably();
  }

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

    DbFuture<Void> closeFuture = connectionManager.close(true);
    closeFuture.getUninterruptably();
  }

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

      connection.close(true);
    }
  }

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

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

   * @throws InterruptedException
   * @throws DbException
   */
  public static void main(String[] args) throws DbException, InterruptedException {
    ConnectionManager cm = ConnectionManagerProvider.createConnectionManager("adbcj:postgresql-mina://localhost/adbcjtck", "adbcjtck", "adbcjtck");
    Connection connection = cm.connect().get();
 
    final ResultSet rs = connection.executeQuery("SELECT * FROM large").get();
    for (Row row : rs) {
      System.out.println(row.get(0) + " " + row.get(1));
    }
    cm.close(true).get();
  }
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.