Is there a way to prevent Spring Data from trying to connect to Cassandra if a certain profile is provided?

27 views Asked by At

I have the following...

@RestController
@RequestMapping("/cassandra")
@AllArgsConstructor
@Profile("cassandra")
public class CassandraController {
    private final MessageRepo messageRepo;

    @GetMapping("/message")
    public Message getMessage() {
        return messageRepo.save(new Message("Hello World"));
    }
}

The idea here is I would like a controller that, assuming the cassandra profile is active, will save a simple message. The problem is that if I run without the cassandra profile then I still get...

Suppressed: com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s0|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io
.netty.channel.StacklessClosedChannelException)
                        at com.datastax.oss.driver.internal.core.channel.ProtocolInitHandler$InitRequest.fail(ProtocolInitHandler.java:356)
                        at com.datastax.oss.driver.internal.core.channel.ChannelHandlerRequest.writeListener(ChannelHandlerRequest.java:87)

I tried adding this...

@Configuration
@Profile("!cassandra")
public class NoCassandraConfig {
    static class NoOpCqlSession implements CqlSession {
        ... methods set to null
    }
    @Bean
    @ConditionalOnMissingBean
    public CqlSession cqlSession() {
        // Return a no-op or dummy CqlSession when the 'cassandra' profile is not active
        return new NoOpCqlSession();
    }
}

But I get the same response. If I run with the cassandra profile everything connects fine (the server is not running on 127.0.0.1). Is there a way to make sure that Spring Data does not try to connect to cassandra if the profile is not provided?

0

There are 0 answers