use of org.adbcj.Connection in project adbcj by mheath.
the class ConnectSpecialCaseTest method testImmediateClose.
// @Parameters({"url", "user", "password"})
// @Test(timeOut=60000)
// public void testConnectCancel(String url, String user, String password) throws Exception {
// StringBuilder urlBuilder = new StringBuilder();
//
// URI connectUrl = new URI(url);
// String scheme = connectUrl.getScheme();
// while (scheme != null) {
// urlBuilder.append(scheme).append(":");
// connectUrl = new URI(connectUrl.getSchemeSpecificPart());
// scheme = connectUrl.getScheme();
// }
//
// urlBuilder.append("//").append(UNREACHABLE_HOST);
// urlBuilder.append(connectUrl.getPath());
//
// final boolean[] callbacks = {false};
// final CountDownLatch latch = new CountDownLatch(1);
//
// ConnectionManager connectionManager = ConnectionManagerProvider.createConnectionManager(urlBuilder.toString(), "dummyuser", "dummypassword");
// try {
// DbFuture<Connection> connectFuture = connectionManager.connect().addListener(new DbListener<Connection>() {
// public void onCompletion(DbFuture<Connection> future) throws Exception {
// callbacks[0] = true;
// latch.countDown();
// }
// });
// assertTrue(connectFuture.cancel(true), "Connection to unreachable host was not canceled");
// assertTrue(connectFuture.isCancelled());
// assertTrue(latch.await(1, TimeUnit.SECONDS), "Callback was not invoked in time");
// assertTrue(callbacks[0], "Connect future callback was not invoked with connect cancellation");
// } finally {
// connectionManager.finalizeClose(true);
// }
// }
@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 {
connectionManager.close(true).get();
}
}
use of org.adbcj.Connection in project adbcj by mheath.
the class ConnectTest method testCancelClose.
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");
}
use of org.adbcj.Connection in project adbcj by mheath.
the class SelectForUpdateTest method testSelectForUpdate.
public void testSelectForUpdate() throws Exception {
logger.debug("Using connection manager: {}", connectionManager);
final boolean[] invoked = { false, false };
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");
}
use of org.adbcj.Connection in project adbcj by mheath.
the class SelectTest method testBrokenSelect.
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();
}
}
use of org.adbcj.Connection in project adbcj by mheath.
the class SelectTest method testSimpleSelect.
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);
}
}
Aggregations