title: NoHostAvailableException :: Java to Cassandra SSL Cluster connection failed
body: <p>I am trying to connect to a 3 node Cassandra cluster from a Java client program, with Cassandra cluster configured with Client to node encryption enabled. I deployed three self signed certificate in all three nodes, and imported the public certificates to each of other nodes as per the document <a href="https://docs.datastax.com/en/cassandra/3.0/cassandra/configuration/secureSSLClientToNode.html/" rel="nofollow noreferrer" title="Client-to-node encryption">Client-to-node encryption</a>. When I run the client program I am getting the following exception. </p>
  
  <pre><code> Exception in thread "main" com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: clm-pun-swpry4/10.133.181.157:9042 (com.datastax.driver.core.exceptions.TransportException: [clm-pun-swpry4/10.133.181.157:9042] Channel has been closed), clm-pun-swpryf/10.133.181.156:9042 (com.datastax.driver.core.exceptions.TransportException: [clm-pun-swpryf/10.133.181.156:9042] Channel has been closed), clm-pun-sqbgda/10.133.172.70:9042 (com.datastax.driver.core.exceptions.TransportException: [clm-pun-sqbgda/10.133.172.70:9042] Channel has been closed))
      at com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:233)
      at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:79)
      at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1483)
      at com.datastax.driver.core.Cluster.init(Cluster.java:159)
      at com.datastax.driver.core.SessionManager.initAsync(SessionManager.java:78)
      at com.datastax.driver.core.SessionManager.executeAsync(SessionManager.java:139)
      at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:68)
      at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:43)
      at clm.bmc.saas.incubator.ClientToNodeExample.main(ClientToNodeExample.java:28)
  </code></pre>
  
  <p>My Cassandra configuration in cassandra.yaml (individual self signed certificate for each of the 3 nodes):</p>
  
  <pre><code>client_encryption_options:
      enabled: true
      # If enabled and optional is set to true encrypted and unencrypted connections are handled.
      optional: false
      keystore: /opt/secure/keystore.clm-pun-swpry4
      keystore_password: changeit
      # require_client_auth: false
      # Set trustore and truststore_password if require_client_auth is true
      # truststore: conf/.truststore
      # truststore_password: cassandra
      # More advanced defaults below:
      protocol: TLSv1.2
      algorithm: SunX509
      store_type: JKS
      cipher_suites: [TLS_RSA_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_256_CBC_SHA]
  </code></pre>
  
  <p>My Java program:</p>
  
  <pre><code>
  public class ClientToNodeExample {
      private static final Logger LOGGER = LoggerFactory.getLogger(ClientToNodeExample.class);
  
      public static void main(String[] args) {
          ClientToNodeExample example = new ClientToNodeExample();
          Session session = example.getCluster("C:\\install\\ssl\\cassandraCluster.ks", "changeit",
                  new String[]{"clm-pun-sqbgda", "clm-pun-swpryf", "clm-pun-swpry4"}, 9042).newSession();
          ResultSet results = session.execute("SELECT * FROM entity_space.mo;");
          LOGGER.info("NumberOfRows:" + results.all().size());
          session.close();
      }
  
      private Cluster getCluster(String trustStoreLocation, String trustStorePassword, String[] host, int port) {
          Cluster cluster;
          SSLContext sslcontext = null;
          try {
              InputStream is = ClientToNodeExample.class.getResourceAsStream(trustStoreLocation);
              KeyStore keystore = KeyStore.getInstance("jks");
              char[] pwd = trustStorePassword.toCharArray();
              keystore.load(is, pwd);
              TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
              tmf.init(keystore);
              TrustManager[] tm = tmf.getTrustManagers();
              sslcontext = SSLContext.getInstance("TLS");
              sslcontext.init(null, tm, null);
          } catch (Exception e) {
              LOGGER.error("ERROR", e);
          }
          JdkSSLOptions sslOptions = JdkSSLOptions.builder().withSSLContext(sslcontext).build();
          cluster = Cluster.builder().addContactPoints(host).withPort(port).withSSL(sslOptions).build();
          return cluster;
      }
  }
  </code></pre>
  
  <p>However if I try to check the certificate from each node with the following keytool command, I get the certificate:</p>
  
  <pre><code>keytool -printcert -sslserver clm-pun-sqbgda:9042 -rfc
  </code></pre>
  
  <p>Can anyone please help where I am going wrong? </p>
  
  <pre><code>Cassandra version:3.11.0
  cassandra-driver-core : 3.1.4
  </code></pre>