public void forceQueRequest(Request request) { synchronized (lock) { if (blockingRequest == null) { requestQueue.add(request); channel.writeAndFlush(request.getRequest()); if (request.isBlocking()) { blockingRequest = new BlockingRequestInProgress(request); request .getToComplete() .addListener( new DbListener<Object>() { @Override public void onCompletion(DbFuture<Object> future) { blockingRequest.continueWithRequests(); } }); } } else { if (blockingRequest.unblockBy(request)) { requestQueue.add(request); channel.writeAndFlush(request.getRequest()); } else { blockingRequest.add(request); } } } }
@Override public DbFuture<PreparedUpdate> prepareUpdate(String sql) { checkClosed(); final Request request = requestCreator.executePrepareUpdate(sql); queRequest(request); return (DbFuture<PreparedUpdate>) request.getToComplete(); }
@Override public DbFuture<Result> executeUpdate(String sql) { checkClosed(); final Request request = requestCreator.executeUpdate(sql); queRequest(request); return (DbFuture) request.getToComplete(); }
@Override public <T> DbFuture<T> executeQuery(String sql, ResultHandler<T> eventHandler, T accumulator) { checkClosed(); synchronized (lock) { final Request request = requestCreator.createQuery(sql, eventHandler, accumulator); queRequest(request); return (DbFuture<T>) request.getToComplete(); } }
@Override public DbFuture<Void> rollback() { checkClosed(); synchronized (lock) { if (!isInTransaction()) { throw new DbException("Not currently in a transaction, cannot rollback"); } final Request request = requestCreator.rollbackTransaction(); queRequest(request); isInTransaction = false; return (DbFuture<Void>) request.getToComplete(); } }
public Request dequeRequest() { synchronized (lock) { final Request request = requestQueue.poll(); if (logger.isDebugEnabled()) { logger.debug("Dequeued request: {}", request); } if (request.getRequest().wasCancelled()) { if (logger.isDebugEnabled()) { logger.debug("Request has been cancelled: {}", request); } return dequeRequest(); } return request; } }
@Override public DbFuture<Void> close(CloseMode closeMode) throws DbException { synchronized (lock) { if (this.closeFuture != null) { return closeFuture; } if (closeMode == CloseMode.CANCEL_PENDING_OPERATIONS) { forceCloseOnPendingRequests(); } Request request = requestCreator.createCloseRequest(); forceQueRequest(request); closeFuture = (DefaultDbFuture<Void>) request.getToComplete(); closeFuture.addListener( new DbListener<Void>() { @Override public void onCompletion(DbFuture<Void> future) { H2Connection.this.manager.removeConnection(H2Connection.this); } }); return closeFuture; } }
private void forceCloseOnPendingRequests() { for (Request request : requestQueue) { if (request.getRequest().tryCancel()) { request .getToComplete() .trySetException(new DbSessionClosedException("Connection is closed")); } } if (null != blockingRequest) { for (Request waitingRequest : blockingRequest.waitingRequests) { if (waitingRequest.getRequest().tryCancel()) { waitingRequest .getToComplete() .trySetException(new DbSessionClosedException("Connection is closed")); } } } }
public boolean unblockBy(Request nextRequest) { return blockingRequest.unblockBy(nextRequest); }