Example #1
0
 private HostConnectionPool addHost(Host host) {
   try {
     HostDistance distance = loadBalancer.distance(host);
     if (distance == HostDistance.IGNORED) {
       return pools.get(host);
     } else {
       logger.debug("Adding {} to list of queried hosts", host);
       return pools.put(host, new HostConnectionPool(host, distance, this));
     }
   } catch (AuthenticationException e) {
     logger.error("Error creating pool to {} ({})", host, e.getMessage());
     host.getMonitor()
         .signalConnectionFailure(new ConnectionException(e.getHost(), e.getMessage()));
     return pools.get(host);
   } catch (ConnectionException e) {
     logger.debug("Error creating pool to {} ({})", host, e.getMessage());
     host.getMonitor().signalConnectionFailure(e);
     return pools.get(host);
   }
 }
  private boolean addConnectionIfUnderMaximum() {

    // First, make sure we don't cross the allowed limit of open connections
    for (; ; ) {
      int opened = open.get();
      if (opened >= options().getMaxConnectionsPerHost(hostDistance)) return false;

      if (open.compareAndSet(opened, opened + 1)) break;
    }

    if (isShutdown()) {
      open.decrementAndGet();
      return false;
    }

    // Now really open the connection
    try {
      connections.add(manager.connectionFactory().open(host));
      signalAvailableConnection();
      return true;
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();
      // Skip the open but ignore otherwise
      open.decrementAndGet();
      return false;
    } catch (ConnectionException e) {
      open.decrementAndGet();
      logger.debug("Connection error to {} while creating additional connection", host);
      if (host.getMonitor().signalConnectionFailure(e)) shutdown();
      return false;
    } catch (AuthenticationException e) {
      // This shouldn't really happen in theory
      open.decrementAndGet();
      logger.error(
          "Authentication error while creating additional connection (error is: {})",
          e.getMessage());
      shutdown();
      return false;
    }
  }