Spring Boot 2.3.1.Release and Cassandra DriverTimeout issue

Viewed 1091

I am upgrading to Spring boot version 2.3.1.Release. So, there is a major change in Spring Data Cassandra due to Java driver version upgrade to v4. I am stuck on application startup because there is a DriverTimeout exception being thrown:

 com.datastax.oss.driver.api.core.DriverTimeoutException: [s0|control|id: 0x8572a9d7, L:/My_IPv4:Random_Port - R:/Cassandra_Server:Port] Protocol initialization request, step 1 (OPTIONS): timed out after 500 ms

My cassandra configuration:

    @Bean(name = "mySession")
     @Primary
      public CqlSession session() {
        
         String containerIpAddress = getContactPoints();
         int containerPort = getPort();
         InetSocketAddress containerEndPoint = new InetSocketAddress(containerIpAddress, containerPort);
         
         return CqlSession.builder().withLocalDatacenter(getLocalDataCenter())
                .addContactPoint(containerEndPoint)
                .withAuthCredentials(dbProperties.getCassandraUserName(), dbProperties.getCassandraPassword())
                .withKeyspace(getKeyspaceName()).build();
    }

I have also tried using DriverConfigLoader option by explicitly setting connection timeout as:

     @Bean(name = "mySession")
     @Primary
      public CqlSession session() {
        
         String containerIpAddress = getContactPoints();
         int containerPort = getPort();
         InetSocketAddress containerEndPoint = new InetSocketAddress(containerIpAddress, containerPort);
         
         DriverConfigLoader loader =
                 DriverConfigLoader.programmaticBuilder()
                     .withDuration(DefaultDriverOption.CONNECTION_CONNECT_TIMEOUT, Duration.ofSeconds(5))
                     .build();

         return CqlSession.builder().withLocalDatacenter(getLocalDataCenter())
                 .withConfigLoader(loader)
                .addContactPoint(containerEndPoint)
                .withAuthCredentials(dbProperties.getCassandraUserName(), dbProperties.getCassandraPassword())
                .withKeyspace(getKeyspaceName()).build();
    }

, but to no avail and same exception is being thrown. My current Spring boot version is 2.2.0.Release and I am not even specifying any timeout there and its working fine. How can this issue be fixed?

1 Answers

Assuming that you are using spring-data-cassandra. You can either configure it entirely through your application properties.

Alternatively you can go the route that you're going and build out your own session. If you're doing it that way, you may want to turn off Spring's autoconfiguration.

@EnableAutoConfiguration(exclude = {
    CassandraDataAutoConfiguration.class })

and then instead of building the CqlSession, use the CqlSessionFactory like:

  @Primary
  @Bean("mySessionFactory")
  public CqlSessionFactoryBean getCqlSession() {
    CqlSessionFactoryBean factory = new CqlSessionFactoryBean();
    factory.setUsername(userName);
    factory.setPassword(password);
    factory.setPort(port);
    factory.setKeyspaceName(keyspaceName);
    factory.setContactPoints(hostList);
    factory.setLocalDatacenter(datacenter);
    return factory;
  }
Related