In My Application, Using the below technologies
Spring boot 2.7.x
spring-boot-starter-data-cassandra 2.7.x
spring batch 5. x
java 11
Configuration
@EnableCassandraRepositories(
basePackages = {"com.package1"},
cassandraTemplateRef = "cassandraTemplate1",
repositoryFactoryBeanClass = CassandraRepositoryWithTtlFactoryBean.class
)
public class CassandraConfigurationCluster1 extends CassandraConfiguration {
Configuration
@EnableCassandraRepositories(
basePackages = {"com.package2"},
cassandraTemplateRef = "cassandraTemplate2",
repositoryFactoryBeanClass = CassandraRepositoryWithTtlFactoryBean.class
)
public class CassandraConfigurationCluster2 extends CassandraConfiguration {
and with that I have created two beans for CassandraAdminTemplate ( not sure I was using CassandraTemplate from spring boot 2.2.x but its changed to something else in 2.7.x
@Bean("session1")
public CqlSessionFactoryBean session() {
return new CqlSessionFactoryBean();
}
@Bean("cassandraTemplate1")
public CassandraAdminOperations cassandraTemplate(
@Qualifier("session1") final CqlSessionFactoryBean session) {
return new CassandraAdminTemplate(session.getObject(), cassandraConverter());
}
@Bean("session2")
public CqlSessionFactoryBean session() {
return new CqlSessionFactoryBean();
}
@Bean("cassandraTemplate2")
public CassandraAdminOperations cassandraTemplate(
@Qualifier("session2") final CqlSessionFactoryBean session) {
return new CassandraAdminTemplate(session.getObject(), cassandraConverter());
}
But I am getting error
Unsatisfied dependency expressed through field 'cassandraTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraTemplate' defined in class path resource [/config/CassandraConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.cassandra.core.CassandraAdminTemplate]: Factory method 'cassandraTemplate' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cassandraSessionFactory' defined in class path resource [/config/CassandraConfiguration.class]: Unsatisfied dependency expressed through method 'cassandraSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.datastax.oss.driver.api.core.CqlSession' available: expected single matching bean but found 3: session1,session2","thread":"main","logger":"org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext"}
I know this error but I don't know what else should I use it instead to managed two separate Cassandra connections
Thanks
I managed to create two unique sessions to connect to two different keyspaces by adding below code
For second session
with this I could connect to two clusters without any issues