use of org.adbcj.DbException in project adbcj by mheath.
the class AbstractDbSession method enqueueTransactionalRequest.
protected <E> DbSessionFuture<E> enqueueTransactionalRequest(Request<E> request) {
// Check to see if we're in a transaction
synchronized (lock) {
if (transaction != null) {
if (transaction.isCanceled()) {
return DefaultDbSessionFuture.createCompletedErrorFuture(this, new DbException(this, "Could not execute request; transaction is in failed state"));
}
// Schedule starting transaction with database if possible
if (!transaction.isBeginScheduled()) {
// Set isolation level if necessary
enqueueStartTransaction(transaction);
transaction.setBeginScheduled(true);
}
transaction.addRequest(request);
}
}
enqueueRequest(request);
return request;
}
use of org.adbcj.DbException in project adbcj by mheath.
the class MysqlConnectionManagerFactory method createConnectionManager.
public ConnectionManager createConnectionManager(String url, String username, String password, Properties properties) throws DbException {
try {
// Parse URL
URI uri = new URI(url);
// Throw away the 'adbcj' protocol part of the URL
uri = new URI(uri.getSchemeSpecificPart());
String host = uri.getHost();
int port = uri.getPort();
if (port < 0) {
port = DEFAULT_PORT;
}
String schema = uri.getPath().substring(1);
return new MysqlConnectionManager(host, port, username, password, schema, properties);
} catch (URISyntaxException e) {
throw new DbException(e);
}
}
use of org.adbcj.DbException in project adbcj by mheath.
the class JdbcConnectionManager method close.
public DbFuture<Void> close(boolean immediate) throws DbException {
synchronized (lock) {
if (closeFuture == null) {
closeFuture = new DefaultDbFuture<Void>();
closeFuture.addListener(new DbListener<Void>() {
@Override
public void onCompletion(DbFuture<Void> future) throws Exception {
executorService.shutdown();
}
});
} else {
return closeFuture;
}
}
final AtomicInteger countDown = new AtomicInteger();
final AtomicBoolean allClosed = new AtomicBoolean(false);
DbListener<Void> listener = new DbListener<Void>() {
@Override
public void onCompletion(DbFuture<Void> future) {
try {
int count = countDown.decrementAndGet();
future.get();
if (allClosed.get() && count == 0) {
closeFuture.setResult(null);
}
} catch (Exception e) {
// If the connection close errored out, error out our closeFuture too
closeFuture.setException(e);
}
}
};
synchronized (lock) {
for (JdbcConnection connection : connections) {
countDown.incrementAndGet();
connection.close(immediate).addListener(listener);
}
}
allClosed.set(true);
if (countDown.get() == 0) {
closeFuture.setResult(null);
}
return closeFuture;
}
use of org.adbcj.DbException in project adbcj by mheath.
the class ProtocolHandler method handleException.
/**
* Handles an exception
*
* @param connection
* @param cause
* @return any exception that couldn't be handled, null if the exception was succesfully handled
* @throws Exception
*/
public Throwable handleException(AbstractMySqlConnection connection, Throwable cause) throws Exception {
logger.debug("Caught exception: ", cause);
DbException dbException = DbException.wrap(connection, cause);
if (connection != null) {
DefaultDbFuture<Connection> connectFuture = connection.getConnectFuture();
if (!connectFuture.isDone()) {
connectFuture.setException(dbException);
return null;
}
Request<?> activeRequest = connection.getActiveRequest();
if (activeRequest != null) {
if (!activeRequest.isDone()) {
try {
activeRequest.error(dbException);
return null;
} catch (Throwable e) {
return e;
}
}
}
}
return dbException;
}
use of org.adbcj.DbException 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");
}
Aggregations