Search in sources :

Example 11 with DbException

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;
}
Also used : DbException(org.adbcj.DbException)

Example 12 with DbException

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);
    }
}
Also used : URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) DbException(org.adbcj.DbException)

Example 13 with DbException

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;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DbListener(org.adbcj.DbListener) DefaultDbFuture(org.adbcj.support.DefaultDbFuture) DbFuture(org.adbcj.DbFuture) DbException(org.adbcj.DbException) SQLException(java.sql.SQLException)

Example 14 with DbException

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;
}
Also used : Connection(org.adbcj.Connection) DbException(org.adbcj.DbException)

Example 15 with 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");
}
Also used : Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) TimeoutException(java.util.concurrent.TimeoutException) DbException(org.adbcj.DbException)

Aggregations

DbException (org.adbcj.DbException)16 Connection (org.adbcj.Connection)7 URI (java.net.URI)5 URISyntaxException (java.net.URISyntaxException)5 SQLException (java.sql.SQLException)2 ResultSet (org.adbcj.ResultSet)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ConnectionManager (org.adbcj.ConnectionManager)1 DbFuture (org.adbcj.DbFuture)1 DbListener (org.adbcj.DbListener)1 DbFutureConcurrentProxy (org.adbcj.support.DbFutureConcurrentProxy)1 DefaultDbFuture (org.adbcj.support.DefaultDbFuture)1 Parameters (org.testng.annotations.Parameters)1 Test (org.testng.annotations.Test)1