private Connection getGoodConnection() { final Connection connection = Mockito.mock(Connection.class); Mockito.when(connection.connect()).thenAnswer(new Answer<CompletableFuture<? extends Connection>>(){ @Override public CompletableFuture<? extends Connection> answer(InvocationOnMock invocation) throws Throwable { return CompletableFuture.completedFuture(connection); } }); Mockito.when(connection.isConnected()).thenReturn(true); return connection; }
public synchronized void expire(Connection connection) { connection.disconnect(); availableConnections.remove(connection); poolSize -= 1; }
@Override public SQLConnection setAutoCommit(boolean autoCommit, Handler<AsyncResult<Void>> handler) { Future<Void> fut; synchronized (this) { if (inTransaction && autoCommit) { inTransaction = false; fut = ConversionUtils.completableFutureToVertxVoid(connection.sendQuery("COMMIT"), vertx); } else { fut = Future.succeededFuture(); } inAutoCommit = autoCommit; } fut.setHandler(handler); return this; }
vertx.cancelTimer(timerId); if (connection.isConnected()) { connection.disconnect(); // drop the connection if it's still alive synchronized (this) { poolSize -= 1; connection.sendQuery("SELECT 1 AS alive") .whenCompleteAsync((ignored, error) -> { if (error != null) { logger.info("connection test failed", error); connection.disconnect(); // try to close the connection synchronized (this) { poolSize -= 1;
public synchronized void giveBack(Connection connection) { if (connection.isConnected()) { availableConnections.add(connection); if (connectionReleaseDelay > 0) { Long timerId = vertx.setTimer(connectionReleaseDelay, res -> expire(connection)); timers.put(connection, timerId); } } else { poolSize -= 1; } notifyWaitersAboutAvailableConnection(); }
private synchronized void createAndConnect(Handler<AsyncResult<Connection>> handler) { try { create() .connect() .whenCompleteAsync((connection, error) -> { try { if (error != null) { logger.info("failed to create connection", error); handler.handle(Future.failedFuture(error)); } else { handler.handle(Future.succeededFuture(connection)); } } catch (Throwable exception) { Handler<Throwable> exceptionHandler = vertx.getOrCreateContext().exceptionHandler(); if (exceptionHandler != null) { exceptionHandler.handle(exception); } else { throw exception; } } }, ConversionUtils.vertxToExecutor(vertx)); } catch (Throwable e) { logger.info("creating a connection went wrong", e); handler.handle(Future.failedFuture(e)); } }
@Override public SQLConnection queryStreamWithParams(String sql, JsonArray params, Handler<AsyncResult<SQLRowStream>> handler) { beginTransactionIfNeeded(v -> { ConversionUtils.connectCompletableFutureWithHandler( connection.sendPreparedStatement(sql, ConversionUtils.WrapList(params)), vertx, handleAsyncQueryResultToRowStream(handler)); }); return this; }
vertx.cancelTimer(timerId); if (connection.isConnected()) { connection.disconnect(); // drop the connection if it's still alive synchronized (this) { poolSize -= 1; connection.sendQuery("SELECT 1 AS alive") .whenCompleteAsync((ignored, error) -> { if (error != null) { logger.info("connection test failed", error); connection.disconnect(); // try to close the connection synchronized (this) { poolSize -= 1;
public synchronized void giveBack(Connection connection) { if (connection.isConnected()) { availableConnections.add(connection); if (connectionReleaseDelay > 0) { Long timerId = vertx.setTimer(connectionReleaseDelay, res -> expire(connection)); timers.put(connection, timerId); } } else { poolSize -= 1; } notifyWaitersAboutAvailableConnection(); }
private synchronized void createAndConnect(Handler<AsyncResult<Connection>> handler) { try { create() .connect() .whenCompleteAsync((connection, error) -> { try { if (error != null) { logger.info("failed to create connection", error); handler.handle(Future.failedFuture(error)); } else { handler.handle(Future.succeededFuture(connection)); } } catch (Throwable exception) { Handler<Throwable> exceptionHandler = vertx.getOrCreateContext().exceptionHandler(); if (exceptionHandler != null) { exceptionHandler.handle(exception); } else { throw exception; } } }, ConversionUtils.vertxToExecutor(vertx)); } catch (Throwable e) { logger.info("creating a connection went wrong", e); handler.handle(Future.failedFuture(e)); } }
@Override public SQLConnection updateWithParams(String sql, JsonArray params, Handler<AsyncResult<UpdateResult>> handler) { beginTransactionIfNeeded(v -> { ConversionUtils.connectCompletableFutureWithHandler( connection.sendPreparedStatement(sql, ConversionUtils.WrapList(params)), vertx, handleAsyncUpdateResultToResultSet(handler)); }); return this; }
private Connection getGoodConnection() { final Connection connection = Mockito.mock(Connection.class); Mockito.when(connection.connect()).thenAnswer(new Answer<CompletableFuture<? extends Connection>>(){ @Override public CompletableFuture<? extends Connection> answer(InvocationOnMock invocation) throws Throwable { return CompletableFuture.completedFuture(connection); } }); Mockito.when(connection.isConnected()).thenReturn(true); return connection; }
@Override public SQLConnection setAutoCommit(boolean autoCommit, Handler<AsyncResult<Void>> handler) { Future<Void> fut; synchronized (this) { if (inTransaction && autoCommit) { inTransaction = false; fut = ConversionUtils.completableFutureToVertxVoid(connection.sendQuery("COMMIT"), vertx); } else { fut = Future.succeededFuture(); } inAutoCommit = autoCommit; } fut.setHandler(handler); return this; }
@Override public SQLConnection updateWithParams(String sql, JsonArray params, Handler<AsyncResult<UpdateResult>> handler) { beginTransactionIfNeeded(v -> { ConversionUtils.connectCompletableFutureWithHandler( connection.sendPreparedStatement(sql, ConversionUtils.WrapList(params)), vertx, handleAsyncUpdateResultToResultSet(handler)); }); return this; }
public synchronized void expire(Connection connection) { connection.disconnect(); availableConnections.remove(connection); poolSize -= 1; }
private synchronized void beginTransactionIfNeeded(Handler<AsyncResult<Void>> action) { if (!inAutoCommit && !inTransaction) { inTransaction = true; ConversionUtils.completableFutureToVertxVoid(connection.sendQuery(getStartTransactionStatement()), vertx) .setHandler(action); } else { action.handle(Future.succeededFuture()); } }
@Override public SQLConnection queryWithParams(String sql, JsonArray params, Handler<AsyncResult<ResultSet>> handler) { beginTransactionIfNeeded(v -> { ConversionUtils.connectCompletableFutureWithHandler( connection.sendPreparedStatement(sql, ConversionUtils.WrapList(params)), vertx, handleAsyncQueryResultToResultSet(handler)); }); return this; }
@Override public SQLConnection execute(String sql, Handler<AsyncResult<Void>> handler) { beginTransactionIfNeeded(v -> { ConversionUtils.connectCompletableFutureWithVoidHandler(connection.sendQuery(sql), vertx, handler); }); return this; }
@Override public SQLConnection queryWithParams(String sql, JsonArray params, Handler<AsyncResult<ResultSet>> handler) { beginTransactionIfNeeded(v -> { ConversionUtils.connectCompletableFutureWithHandler( connection.sendPreparedStatement(sql, ConversionUtils.WrapList(params)), vertx, handleAsyncQueryResultToResultSet(handler)); }); return this; }
private synchronized void beginTransactionIfNeeded(Handler<AsyncResult<Void>> action) { if (!inAutoCommit && !inTransaction) { inTransaction = true; ConversionUtils.completableFutureToVertxVoid(connection.sendQuery(getStartTransactionStatement()), vertx) .setHandler(action); } else { action.handle(Future.succeededFuture()); } }