title: Connection Pool and Database Availability
body: <p>If I've a connection pool to the DB and the DB is restarted, the connections become invalid. Why? The DB hostname hasn't changed. How can I detect this situation and purge the invalid connections?
  The DB in my case is Couchbase but I'd think that this is a general problem. We're using Spring framework with Couchbase Java client for the following. </p>
  
  <p>Couchbase configuration:</p>
  
  <pre><code>@Configuration
  public class CouchbaseConfiguration {
      private Cluster cluster;
  
      @Value("${couchbase.nodes}")
      @Setter(AccessLevel.PACKAGE)
      private String nodes;
  
      @Value("${couchbase.adminUsername}")
      private String adminUsername;
  
      @Value("${couchbase.adminPassword}")
      private String adminPassword;
  
      private static final long CONNECT_TIMEOUT = TimeUnit.SECONDS.toMillis(10);
  
      @Setter(AccessLevel.PACKAGE)
      private Func2&lt;CouchbaseEnvironment, List&lt;String&gt;, Cluster&gt; clusterFactory = CouchbaseCluster::create; //NOPMD
  
      @PostConstruct
      void postConstruct() {
          List&lt;String&gt; nodes = asList(this.nodes.split(",")).stream().map(String::trim).collect(toList());
  
          CouchbaseEnvironment env = DefaultCouchbaseEnvironment.builder().connectTimeout(CONNECT_TIMEOUT)
                  .bootstrapCarrierEnabled(false).build();
  
          cluster = clusterFactory.call(env, nodes);
      }
  
      @Bean
      Cluster cluster() {
          return cluster;
      }
  
      @Bean
      ClusterManager clusterManager() {
          return cluster.clusterManager(adminUsername, adminPassword);
      }
  
      @PreDestroy
      void preDestroy() {
          cluster.disconnect();
      }
  }
  </code></pre>
  
  <p>Couchbase usage:</p>
  
  <pre><code>@Autowired
  private Cluster cluster;
  
  @Bean
  Bucket bucket() {
      return cluster.openBucket(bucketName, bucketPassword);
  }
  </code></pre>