@Override
 public Connection getConnection() {
   isTrue("open", !isClosed());
   try {
     return connectionFactory.create(
         connectionPool.get(), new DefaultServerProtocolExecutor(), clusterConnectionMode);
   } catch (MongoSecurityException e) {
     invalidate();
     throw e;
   }
 }
  private void _sendAndHandle(
      ConnectionFactory connectionFactory, Object obj, ResponseHandler handler) throws IOException {
    ResponseStream stream = null;
    while (stream == null) {
      if (!active) {
        throw new HazelcastInstanceNotActiveException();
      }
      Connection conn = null;
      try {
        conn = connectionFactory.create();
        final SerializationService serializationService = getSerializationService();
        final Data request = serializationService.toData(obj);
        conn.write(request);
        stream = new ResponseStreamImpl(serializationService, conn);
      } catch (Exception e) {
        if (e instanceof IOException) {
          if (logger.isFinestEnabled()) {
            logger.finest("Error on connection... conn: " + conn + ", error: " + e);
          }
        }
        if (conn != null) {
          IOUtil.closeResource(conn);
        }
        if (ErrorHandler.isRetryable(e)) {
          if (redoOperation || obj instanceof RetryableRequest) {
            if (logger.isFinestEnabled()) {
              logger.finest("Retrying " + obj + ", last-conn: " + conn + ", last-error: " + e);
            }
            beforeRetry();
            continue;
          }
        }
        if (e instanceof IOException && !active) {
          continue;
        }
        throw ExceptionUtil.rethrow(e, IOException.class);
      }
    }

    try {
      handler.handle(stream);
    } catch (Exception e) {
      throw ExceptionUtil.rethrow(e, IOException.class);
    } finally {
      stream.end();
    }
  }
 private <T> T _sendAndReceive(ConnectionFactory connectionFactory, Object obj)
     throws IOException {
   while (active) {
     Connection conn = null;
     boolean release = true;
     try {
       conn = connectionFactory.create();
       final SerializationService serializationService = getSerializationService();
       final Data request = serializationService.toData(obj);
       conn.write(request);
       final Data response = conn.read();
       final Object result = serializationService.toObject(response);
       return ErrorHandler.returnResultOrThrowException(result);
     } catch (Exception e) {
       if (e instanceof IOException) {
         if (logger.isFinestEnabled()) {
           logger.finest("Error on connection... conn: " + conn + ", error: " + e);
         }
         IOUtil.closeResource(conn);
         release = false;
       }
       if (ErrorHandler.isRetryable(e)) {
         if (redoOperation || obj instanceof RetryableRequest) {
           if (logger.isFinestEnabled()) {
             logger.finest("Retrying " + obj + ", last-conn: " + conn + ", last-error: " + e);
           }
           beforeRetry();
           continue;
         }
       }
       if (e instanceof IOException && !active) {
         continue;
       }
       throw ExceptionUtil.rethrow(e, IOException.class);
     } finally {
       if (release && conn != null) {
         conn.release();
       }
     }
   }
   throw new HazelcastInstanceNotActiveException();
 }
  public ProjectConnection connect() throws GradleConnectionException {
    LOGGER.debug(
        "Connecting from tooling API consumer version {}", GradleVersion.current().getVersion());

    if (projectDir == null) {
      throw new IllegalStateException(
          "A project directory must be specified before creating a connection.");
    }
    if (distribution == null) {
      distribution = distributionFactory.getDefaultDistribution(projectDir);
    }
    return connectionFactory.create(
        distribution,
        new DefaultConnectionParameters(
            projectDir,
            gradleUserHomeDir,
            searchUpwards,
            embedded,
            daemonMaxIdleTimeValue,
            daemonMaxIdleTimeUnits));
  }