Search in sources :

Example 1 with DbException

use of org.adbcj.DbException in project adbcj by mheath.

the class MinaConnectionManagerFactory 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 MinaConnectionManager(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 2 with DbException

use of org.adbcj.DbException in project adbcj by mheath.

the class NettyConnectionManagerFactory 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 NettyConnectionManager(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 3 with DbException

use of org.adbcj.DbException in project adbcj by mheath.

the class ConnectSpecialCaseTest method testConnectBadCredentials.

@Parameters({ "url", "user", "password" })
@Test(timeOut = 60000)
public void testConnectBadCredentials(String url, String user, String password) throws InterruptedException {
    final boolean[] callbacks = { false };
    final CountDownLatch latch = new CountDownLatch(1);
    ConnectionManager connectionManager = ConnectionManagerProvider.createConnectionManager(url, user, "__BADPASSWORD__");
    try {
        DbFuture<Connection> connectFuture = connectionManager.connect().addListener(new DbListener<Connection>() {

            public void onCompletion(DbFuture<Connection> future) throws Exception {
                callbacks[0] = true;
                latch.countDown();
            }
        });
        try {
            connectFuture.get();
            fail("Connect should have failed because of bad credentials");
        } catch (DbException e) {
            assertTrue(connectFuture.isDone(), "Connect future should be marked done even though it failed");
            assertTrue(!connectFuture.isCancelled(), "Connect future should not be marked as cancelled");
        }
        assertTrue(latch.await(1, TimeUnit.SECONDS), "Callback was not invoked in time");
        assertTrue(callbacks[0], "Connect future callback was not invoked with connect failure");
    } finally {
        connectionManager.close(true);
    }
}
Also used : ConnectionManager(org.adbcj.ConnectionManager) Connection(org.adbcj.Connection) CountDownLatch(java.util.concurrent.CountDownLatch) DbException(org.adbcj.DbException) DbException(org.adbcj.DbException) Parameters(org.testng.annotations.Parameters) Test(org.testng.annotations.Test)

Example 4 with DbException

use of org.adbcj.DbException in project adbcj by mheath.

the class TransactionTest method testCommitRollbackWithNoTransaction.

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

Example 5 with DbException

use of org.adbcj.DbException in project adbcj by mheath.

the class TransactionTest method testBeginTransaction.

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