title: NoHostAvailableException: All host(s) tried for query failed (no host was tried) followed by SocketException
body: <p>I have recently observed the <strong>NoHostAvailableException: All host(s) tried for query failed (no host was tried)</strong> followed by <strong>java.net.SocketException: Software caused connection abort: recv failed</strong> on two different sites which were running smoothly since last 8-10 Days. I checked other related questions about the same but they were having different issues like multiple session and Socket Timeout.I don't know what could be the reason. Here is my code for connection </p>
  
  <pre><code>public class CassandraConnector {
  
      private Cluster cluster;
      private Session session;
  
      /**
       * No-Args constructor
       */
      private CassandraConnector() {
  
      }
  
      /**
       * static method to return the instance of CassandraConnector using static
       * class CassandraConnectorHolder's static INSTANCE
       *
       * @return static instance of CassandraConnector
       */
      public static CassandraConnector getInstance() {
  
          return CassandraConnectorHolder.INSTANCE;
      }
  
      private static class CassandraConnectorHolder {
  
          private static final CassandraConnector INSTANCE = new CassandraConnector();
      }
  
      /**
       * returns the instance of Cassandra Session
       * {@link com.datastax.driver.core.Session}
       *
       * @return instance of Cassandra Session
       * {@link com.datastax.driver.core.Session}
       */
      public Session getSession() {
          return session;
      }
  
      /**
       * This method is used to close the Cassandra session and cluster
       */
      public void close() {
          session.close();
          cluster.close();
      }
  
      /**
       * This method is used to connect to Cassandra cluster using
       * ipAddress(HostName) passed as parameter
       *
       * @param ipAddress String
       */
      public void connect(String ipAddress) {
          //cluster = Cluster.builder().addContactPoint("127.0.0.1").withPort(9042).build();
  
          SocketOptions options = new SocketOptions();
          options.setConnectTimeoutMillis(60000);//1 minute
          options.setReadTimeoutMillis(120000);//2 minute
          options.setTcpNoDelay(true);
          cluster = Cluster.builder().addContactPoint(ipAddress).withPort(9042).withSocketOptions(options).build();
  
          final Metadata metadata = cluster.getMetadata();
  
          System.out.println("Connected to cluster: " + metadata.getClusterName());
          for (final Host host : metadata.getAllHosts()) {
  
              System.out.println("Datacenter: " + host.getDatacenter() + "Host:" + host.getAddress() + " Rack: " + host.getRack());
          }
          session = cluster.connect();
      }
  }
  </code></pre>
  
  <p>and here is the log I found it was same on both the sites.</p>
  
  <pre><code>java.net.SocketException: Software caused connection abort: recv failed
      at java.net.SocketInputStream.socketRead0(Native Method) ~[?:1.8.0_121]
      at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) ~[?:1.8.0_121]
      at java.net.SocketInputStream.read(SocketInputStream.java:171) ~[?:1.8.0_121]
      at java.net.SocketInputStream.read(SocketInputStream.java:141) ~[?:1.8.0_121]
      at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) ~[?:1.8.0_121]
      at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) ~[?:1.8.0_121]
      at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) ~[?:1.8.0_121]
      at java.io.InputStreamReader.read(InputStreamReader.java:184) ~[?:1.8.0_121]
      at java.io.BufferedReader.fill(BufferedReader.java:161) ~[?:1.8.0_121]
      at java.io.BufferedReader.readLine(BufferedReader.java:324) ~[?:1.8.0_121]
      at java.io.BufferedReader.readLine(BufferedReader.java:389) ~[?:1.8.0_121]
      at marc.socket.LoggerServer$ServeRequest.run(LoggerServer.java:136) [MARC%20Logger.jar:1.0]
  2020-03-06 22:05:53 INFO  [meter-reader] ModbusMeterReader:592 - Closed Serial for 33967
  2020-03-06 22:05:53 INFO  [meter-reader] ModbusMeterReader:659 - Final Object {"meter":"33967","slot":"2020-03-06 22:05:44.909","E":"E"}
  2020-03-06 22:05:53 DEBUG [meter-reader] LoggerServer:320 - Sent Data from Logger Server
  2020-03-06 22:05:53 ERROR [meter-reader] LoggerServer:324 - PrintWriter has error while sending
  2020-03-06 22:05:53 ERROR [meter-reader] LoggerServer:326 - Added socket to dead list
  2020-03-06 22:05:53 ERROR [meter-reader] ModbusMeterReader:697 - Error
  com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (no host was tried)
      at com.datastax.driver.core.exceptions.NoHostAvailableException.copy(NoHostAvailableException.java:84) ~[cassandra-driver-core-3.4.0.jar:?]
      at com.datastax.driver.core.exceptions.NoHostAvailableException.copy(NoHostAvailableException.java:37) ~[cassandra-driver-core-3.4.0.jar:?]
      at com.datastax.driver.core.DriverThrowables.propagateCause(DriverThrowables.java:37) ~[cassandra-driver-core-3.4.0.jar:?]
      at com.datastax.driver.core.DefaultResultSetFuture.getUninterruptibly(DefaultResultSetFuture.java:245) ~[cassandra-driver-core-3.4.0.jar:?]
      at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:68) ~[cassandra-driver-core-3.4.0.jar:?]
      at com.datastax.driver.core.AbstractSession.execute(AbstractSession.java:59) ~[cassandra-driver-core-3.4.0.jar:?]
      at marc.util.CassandraDatabase.insertData(CassandraDatabase.java:92) ~[MARC%20Logger.jar:1.0]
      at marc.logger.ModbusMeterReader.startReading(ModbusMeterReader.java:678) ~[MARC%20Logger.jar:1.0]
      at marc.controllers.ControlPanelController.startAcquisition(ControlPanelController.java:1226) ~[MARC%20Logger.jar:1.0]
      at marc.controllers.ControlPanelController$3.lambda$run$0(ControlPanelController.java:1170) ~[MARC%20Logger.jar:1.0]
      at java.util.Iterator.forEachRemaining(Iterator.java:116) [?:1.8.0_121]
      at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801) [?:1.8.0_121]
      at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580) [?:1.8.0_121]
      at marc.controllers.ControlPanelController$3.run(ControlPanelController.java:1168) [MARC%20Logger.jar:1.0]
      at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_121]
      at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [?:1.8.0_121]
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [?:1.8.0_121]
      at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [?:1.8.0_121]
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [?:1.8.0_121]
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [?:1.8.0_121]
      at java.lang.Thread.run(Thread.java:745) [?:1.8.0_121]
  </code></pre>
  
  <p>Is it related to Socket failure or is it related to Cassandra. Do I have to add Socket Timeout option for Cassandra ?</p>