title: Cassandra NoHostAvailableException Java-CQLDriver
body: <p>I am building an application in java using Cassandra's CQL driver. I am able to run it properly when I use local cassandra server. However the same code when I try to run is not working for remote cassandra server. It gives the following error during initialization :- </p>
  
  <p>Following are the maven dependecy that I am using for CQL driver:-</p>
  
  <pre><code>&lt;dependency&gt;
          &lt;groupId&gt;com.datastax.cassandra&lt;/groupId&gt;
          &lt;artifactId&gt;cassandra-driver-core&lt;/artifactId&gt;
          &lt;version&gt;2.1.3&lt;/version&gt;
      &lt;/dependency&gt;
  
      &lt;dependency&gt;
          &lt;groupId&gt;com.datastax.cassandra&lt;/groupId&gt;
          &lt;artifactId&gt;cassandra-driver-mapping&lt;/artifactId&gt;
          &lt;version&gt;2.1.2&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>Following is the error:- </p>
  
  <pre><code>com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: yyyy.yyy.yyy.yyy.yyy-yyy.net/yy.yy.yy.yy:9160 (com.datastax.driver.core.ConnectionException: [yyyy.yyy.yyy.yyy.yyy-yyy.net/yy.yy.yy.yy:9160] Unexpected error during transport initialization (com.datastax.driver.core.ConnectionException: [yyyy.yyy.yyy.yyy.yyy-yyy.net/yy.yy.yy.yy:9160] Operation timed out)), 
      Unexpected error during transport initialization      (com.datastax.driver.core.ConnectionException: [xxxx.xxx.xxx.xxx.xxx-xxx.net/xx.xx.xx.xx:9160] Operation timed out)))
           at   com.datastax.driver.core.ControlConnection.reconnectInternal(ControlConnection.java:220)
      at com.datastax.driver.core.ControlConnection.connect(ControlConnection.java:78)
      at com.datastax.driver.core.Cluster$Manager.init(Cluster.java:1231)
      at com.datastax.driver.core.Cluster.init(Cluster.java:158)
      at com.datastax.driver.core.Cluster.connect(Cluster.java:246)
  </code></pre>
  
  <p>The local cassandra server on which everything works fine is:-</p>
  
  <pre><code>cqlsh 4.1.1, Cassandra 2.0.7 , CQL spec: 3.1.1, Thrift Protocol 19.39.0
  </code></pre>
  
  <p>The remote cassandra server on which connection is getting timed out is:-</p>
  
  <pre><code>cqlsh 3.1.8 | Cassandra 1.2.12 | CQL spec 3.0.0 | Thrift protocol 19.36.1
  </code></pre>
  
  <p>I am using the following code to connect. Can someone comment what is going wrong in the remote connection case?</p>
  
  <pre><code>clusterOBJECT = Cluster.builder().addContactPointsWithPorts(addrs)
                  .withQueryOptions(new     QueryOptions().setConsistencyLevel(ConsistencyLevel.ONE))
                  .withProtocolVersion(1).withoutJMXReporting()
                  .withPort(port)
                  .withSocketOptions(options)
                  .withRetryPolicy(DefaultRetryPolicy.INSTANCE)
                  .withLoadBalancingPolicy(new TokenAwarePolicy(new DCAwareRoundRobinPolicy())).build();
          session = clusterOBJECT.connect();
  </code></pre>
  
  <p>I am providing valid hostname along with port(9160) in addContactPointsWithPorts. Also the hostnames are pingable.</p>
  
  <p>PS:-  The following properties are already set in cassandra.yaml.</p>
  
  <pre><code>start_native_transport: true
  native_transport_port: 9042
  </code></pre>
  
  <p>[EDIT]
  After changing the port to 9042, I was able to connect to Cassandra. BUT I see that the connection is getting dropped by this exception soon after.</p>
  
  <pre><code>com.datastax.driver.core.TransportException: [/xx.xx.xxx.xx:9042] Cannot connect
      at com.datastax.driver.core.Connection.&lt;init&gt;(Connection.java:106)
      at com.datastax.driver.core.PooledConnection.&lt;init&gt;(PooledConnection.java:35)
      at com.datastax.driver.core.Connection$Factory.open(Connection.java:528)
      at com.datastax.driver.core.DynamicConnectionPool.&lt;init&gt;(DynamicConnectionPool.java:74)
      at com.datastax.driver.core.HostConnectionPool.newInstance(HostConnectionPool.java:33)
      at com.datastax.driver.core.SessionManager.replacePool(SessionManager.java:270)
      at com.datastax.driver.core.SessionManager.access$400(SessionManager.java:39)
      at com.datastax.driver.core.SessionManager$3.call(SessionManager.java:303)
      at com.datastax.driver.core.SessionManager$3.call(SessionManager.java:295)
      at java.util.concurrent.FutureTask.run(FutureTask.java:262)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
      at java.lang.Thread.run(Thread.java:745)
   Caused by: java.net.ConnectException: Connection refused: /xx.xx.xxx.xx:9042
      at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
      at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:739)
      at org.jboss.netty.channel.socket.nio.NioClientBoss.connect(NioClientBoss.java:150)
      at org.jboss.netty.channel.socket.nio.NioClientBoss.processSelectedKeys(NioClientBoss.java:105)
      at org.jboss.netty.channel.socket.nio.NioClientBoss.process(NioClientBoss.java:79)
      at org.jboss.netty.channel.socket.nio.AbstractNioSelector.run(AbstractNioSelector.java:318)
      at org.jboss.netty.channel.socket.nio.NioClientBoss.run(NioClientBoss.java:42)
      ... 3 more
  </code></pre>
  
  <p>Thanks.</p>