Cassandra Connection Error [com.datastax.oss.driver.api.core.AllNodesFailedException:]

108 Views Asked by At

I am trying to create multiple sessions in spring application with datastax java driver (4.3.1). My cassandra version is 3.11.2. I will use ipv6 because of that, i upgraded my spring-data-cassadnra version from 1.5.23.RELEASE to 3.4.18(I use java 1.8 if i upgrade to 4.x, i get errors related to that) as suggested here(Cassandra datastax ipv6 connection).

This is a very big project(kind of monolith) so dealing with dependecies is very difficult. Netty dependecies are also a problem i had to manage independently.

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.datastax.oss.driver.api.core.CqlSession]: Factory method 'session' threw exception; nested exception is com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 1, use getErrors() for more: Node(endPoint=localhost/127.0.0.1:9042, hostId=null, hashCode=5e7fdde3): com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s0|control|connecting...] init query OPTIONS: error writing )

Caused by: com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 1, use getErrors() for more: Node(endPoint=localhost/127.0.0.1:9042, hostId=null, hashCode=5e7fdde3): com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s0|control|connecting...] init query OPTIONS: error writing )

I get this error. I have seen some people face issue but their solution didnt work for me like datacenter configuration.

@Configuration
@PropertySource(value = { "classpath:nms.properties" })
public abstract class CassandraConfig {

  protected static final Logger logger = LoggerFactory.getLogger(CassandraConfig.class);

  @Autowired
  protected Environment environment;

  public abstract String getKeyspace();

  public abstract CqlSession session() throws Exception;

  public abstract CassandraOperations cassandraTemplate(CqlSession sessionFactory, CassandraConverter converter) throws Exception;

  public CqlSession getSession() throws Exception {
    return CqlSession.builder()
        .withConfigLoader(
            DriverConfigLoader.programmaticBuilder()
                .withString(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, "datacenter1")
                .withString(DefaultDriverOption.PROTOCOL_VERSION, "V4")
                .withStringList(
                    DefaultDriverOption.CONTACT_POINTS,
                    getContactPoints())
                .build())
        .build();
  }

  protected List<String> getContactPoints() {
    //return Arrays.stream(environment.getProperty("cassandra.contactpoints").split(",")).map(c -> c + ":" + getPort()).collect(Collectors.toList());
    return Collections.singletonList("localhost:9042");
  }

  @Bean
  public CassandraMappingContext mappingContext() {
    return new BasicCassandraMappingContext();
  }

  @Bean
  public CassandraConverter converter(CqlSession cqlSession, CassandraMappingContext mappingContext) {
    MappingCassandraConverter converter = new MappingCassandraConverter(mappingContext);
    converter.setUserTypeResolver(new SimpleUserTypeResolver(cqlSession));
    return converter;
  }

  protected int getPort() {
    return Integer.parseInt(environment.getProperty("cassandra.port", "9042"));
  }
}

@Configuration
public class CassandraNmsConfig extends CassandraConfig {

  @Override
  @Bean(name = "nmsSession")
  public CqlSession session() throws Exception {
    return getSession();
  }

  @Override
  @Bean(name = "nmsTemplate")
  public CassandraAdminOperations cassandraTemplate(@Qualifier("nmsSession") CqlSession session , CassandraConverter converter) throws Exception {
    return new CassandraAdminTemplate(session,converter);
  }

  @Override
  public String getKeyspace() {
    return environment.getProperty("cassandra.keyspace.nms", "nmsdb");
  }
}

I need to create multiple session and I tried with CqlSessionFactory as well but always got same error. I created another simple project with same drivers (spring-cassadnra-data 3.4.18 and with same datastax driver 4.3.1) and I was able to connect to cassandra

public class Main {
  public static void main(String[] args) {
    // Replace with your actual IPv6 address and other configuration
    String query = "SELECT release_version FROM system.local";

    // Create InetAddress object from the IPv6 address

    // Build CqlSession
    CqlSession session = CqlSession.builder()
        .withConfigLoader(
            DriverConfigLoader.programmaticBuilder()
                .withString(DefaultDriverOption.LOAD_BALANCING_LOCAL_DATACENTER, "datacenter1")
                .withStringList(
                    DefaultDriverOption.CONTACT_POINTS,
                    Collections.singletonList("localhost:9042"))
                .build())
        .build();

    try {
      // Execute a simple query
      ResultSet resultSet = session.execute(query);

      // Process the result
      for (Row row : resultSet) {
        String releaseVersion = row.getString("release_version");
        System.out.println("Release Version: " + releaseVersion);
      }

      // Add more queries or operations as needed

    } finally {
      // Close the session when done
      session.close();
    }

  }
}

Release Version: 3.11.2

What might be the problem ? I got stuck here. Thank you.

0

There are 0 best solutions below