Search in sources :

Example 11 with Connection

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

the class JdbcConnectionManager method connect.

public DbFuture<Connection> connect() throws DbException {
    if (isClosed()) {
        throw new DbException("This connection manager is closed");
    }
    final DbFutureConcurrentProxy<Connection> future = new DbFutureConcurrentProxy<Connection>();
    Future<Connection> executorFuture = executorService.submit(new Callable<Connection>() {

        public Connection call() throws Exception {
            try {
                java.sql.Connection jdbcConnection = DriverManager.getConnection(jdbcUrl, properties);
                JdbcConnection connection = new JdbcConnection(JdbcConnectionManager.this, jdbcConnection);
                synchronized (lock) {
                    if (isClosed()) {
                        connection.close(true);
                        future.setException(new DbException("Connection manager closed"));
                    } else {
                        connections.add(connection);
                        future.setValue(connection);
                    }
                }
                return connection;
            } catch (SQLException e) {
                future.setException(new DbException(e));
                e.printStackTrace();
                throw e;
            } finally {
                future.setDone();
            }
        }
    });
    future.setFuture(executorFuture);
    return future;
}
Also used : DbFutureConcurrentProxy(org.adbcj.support.DbFutureConcurrentProxy) SQLException(java.sql.SQLException) Connection(org.adbcj.Connection) DbException(org.adbcj.DbException) SQLException(java.sql.SQLException) DbException(org.adbcj.DbException)

Example 12 with Connection

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

the class PgTest method main.

/**
	 * @param args
	 * @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");
}
Also used : ConnectionManager(org.adbcj.ConnectionManager) Connection(org.adbcj.Connection) ResultSet(org.adbcj.ResultSet) Row(org.adbcj.Row)

Example 13 with Connection

use of org.adbcj.Connection 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 14 with Connection

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

the class ProtocolHandler method handleOkResponse.

private void handleOkResponse(AbstractMySqlConnection connection, OkResponse response) {
    logger.trace("Response '{}' on connection {}", response, connection);
    List<String> warnings = null;
    if (response.getWarningCount() > 0) {
        warnings = new LinkedList<String>();
        for (int i = 0; i < response.getWarningCount(); i++) {
            warnings.add(response.getMessage());
        }
    }
    logger.warn("Warnings: {}", warnings);
    Request<Result> activeRequest = connection.getActiveRequest();
    if (activeRequest == null) {
        // TODO Do we need to pass the warnings on to the connection?
        DefaultDbFuture<Connection> connectFuture = connection.getConnectFuture();
        if (!connectFuture.isDone()) {
            connectFuture.setResult(connection);
            return;
        } else {
            throw new IllegalStateException("Received an OkResponse with no activeRequest " + response);
        }
    }
    Result result = new DefaultResult(response.getAffectedRows(), warnings);
    activeRequest.complete(result);
}
Also used : Connection(org.adbcj.Connection) DefaultResult(org.adbcj.support.DefaultResult) DefaultResult(org.adbcj.support.DefaultResult) Result(org.adbcj.Result)

Example 15 with Connection

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

the class PopulateLarge method main.

public static void main(String[] args) throws Exception {
    ConnectionManager mysqlCM = ConnectionManagerProvider.createConnectionManager("adbcj:mysqlnetty://localhost/adbcjtck", "adbcjtck", "adbcjtck");
    ConnectionManager pgCM = ConnectionManagerProvider.createConnectionManager("adbcj:postgresql-netty://localhost/adbcjtck", "adbcjtck", "adbcjtck");
    Connection mysql = mysqlCM.connect().getUninterruptably();
    Connection pg = pgCM.connect().getUninterruptably();
    final String insertTemplate = "INSERT INTO large (a, b, c) VALUES ('%s', '%s', '%s')";
    for (int i = 0; i < 998; i++) {
        String a = randString();
        String b = randString();
        String c = randString();
        final String insert = String.format(insertTemplate, a, b, c);
        mysql.executeUpdate(insert).get();
        pg.executeUpdate(insert).get();
    }
//		mysql.close(false).get();
//		pg.close(false).get();
//		mysqlCM.close(true);
//		pgCM.close(true);
}
Also used : ConnectionManager(org.adbcj.ConnectionManager) Connection(org.adbcj.Connection)

Aggregations

Connection (org.adbcj.Connection)21 ResultSet (org.adbcj.ResultSet)11 DbException (org.adbcj.DbException)8 CountDownLatch (java.util.concurrent.CountDownLatch)5 ConnectionManager (org.adbcj.ConnectionManager)5 TimeoutException (java.util.concurrent.TimeoutException)4 DbFuture (org.adbcj.DbFuture)4 Result (org.adbcj.Result)4 DbListener (org.adbcj.DbListener)3 ArrayList (java.util.ArrayList)2 DbSessionFuture (org.adbcj.DbSessionFuture)2 Row (org.adbcj.Row)2 Value (org.adbcj.Value)2 Parameters (org.testng.annotations.Parameters)2 Test (org.testng.annotations.Test)2 SQLException (java.sql.SQLException)1 LinkedList (java.util.LinkedList)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 DbFutureConcurrentProxy (org.adbcj.support.DbFutureConcurrentProxy)1 DefaultResult (org.adbcj.support.DefaultResult)1