@Override
 public void onResult(final T result, final Throwable t) {
   for (ReferenceCounted cur : referenceCounted) {
     cur.release();
   }
   wrapped.onResult(result, t);
 }
 @Override
 public void executeAsync(
     final InternalConnection connection,
     final SingleResultCallback<WriteConcernResult> callback) {
   try {
     if (LOGGER.isDebugEnabled()) {
       LOGGER.debug(
           format(
               "Asynchronously inserting %d documents into namespace %s on connection [%s] to server %s",
               insertRequestList.size(),
               getNamespace(),
               connection.getDescription().getConnectionId(),
               connection.getDescription().getServerAddress()));
     }
     super.executeAsync(
         connection,
         new SingleResultCallback<WriteConcernResult>() {
           @Override
           public void onResult(final WriteConcernResult result, final Throwable t) {
             if (t != null) {
               callback.onResult(null, t);
             } else {
               LOGGER.debug("Asynchronous insert completed");
               callback.onResult(result, null);
             }
           }
         });
   } catch (Throwable t) {
     callback.onResult(null, t);
   }
 }
    @Override
    protected void callCallback(
        final ResponseBuffers responseBuffers, final Throwable throwableFromCallback) {
      try {
        if (throwableFromCallback != null) {
          throw throwableFromCallback;
        }

        if (LOGGER.isDebugEnabled()) {
          LOGGER.debug("Command execution completed");
        }

        if (!ProtocolHelper.isCommandOk(
            new BsonBinaryReader(new ByteBufferBsonInput(responseBuffers.getBodyByteBuffer())))) {
          throw getCommandFailureException(
              getResponseDocument(responseBuffers, message, new BsonDocumentCodec()),
              connectionDescription.getServerAddress());
        }

        if (commandListener != null) {
          sendSucceededEvent(
              connectionDescription,
              startTimeNanos,
              message,
              getResponseDocument(responseBuffers, message, new RawBsonDocumentCodec()));
        }
        callback.onResult(
            getResponseDocument(responseBuffers, message, commandResultDecoder), null);

      } catch (Throwable t) {
        sendFailedEvent(connectionDescription, startTimeNanos, message, t);
        callback.onResult(null, t);
      } finally {
        if (responseBuffers != null) {
          responseBuffers.close();
        }
      }
    }
 @Override
 public void executeAsync(
     final InternalConnection connection, final SingleResultCallback<T> callback) {
   long startTimeNanos = System.nanoTime();
   CommandMessage message =
       new CommandMessage(
           namespace.getFullName(),
           command,
           slaveOk,
           fieldNameValidator,
           ProtocolHelper.getMessageSettings(connection.getDescription()));
   boolean sentStartedEvent = false;
   try {
     if (LOGGER.isDebugEnabled()) {
       LOGGER.debug(
           format(
               "Asynchronously sending command {%s : %s} to database %s on connection [%s] to server %s",
               getCommandName(),
               command.values().iterator().next(),
               namespace.getDatabaseName(),
               connection.getDescription().getConnectionId(),
               connection.getDescription().getServerAddress()));
     }
     ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(connection);
     int documentPosition =
         ProtocolHelper.encodeMessageWithMetadata(message, bsonOutput).getFirstDocumentPosition();
     sendStartedEvent(connection, bsonOutput, message, documentPosition);
     sentStartedEvent = true;
     SingleResultCallback<ResponseBuffers> receiveCallback =
         new CommandResultCallback(callback, message, connection.getDescription(), startTimeNanos);
     connection.sendMessageAsync(
         bsonOutput.getByteBuffers(),
         message.getId(),
         new SendMessageCallback<T>(
             connection,
             bsonOutput,
             message,
             getCommandName(),
             startTimeNanos,
             commandListener,
             callback,
             receiveCallback));
   } catch (Throwable t) {
     if (sentStartedEvent) {
       sendFailedEvent(connection.getDescription(), startTimeNanos, message, t);
     }
     callback.onResult(null, t);
   }
 }