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;
}
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");
}
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;
}
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);
}
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);
}
Aggregations