In my springboot application I want to connect to two datasources, a cassandra DB and an oracle DB.
So, I have added the below to my pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
Here is the repository for the oracle DB:
import org.springframework.data.jdbc.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface TestRepository extends ReadRepository<TestEntity, String> {
@Query("select * from test_table where id = :distributorId fetch first 10 rows only")
List<TestEntity> getResult(@Param("distributorId") String distributorId);
}
ReadRepository extends Repository
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.Repository;
@NoRepositoryBean
public interface ReadRepository<T, ID> extends Repository<T, ID> {}
Well this runs fine when I comment out the cassandra dependancy:
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency> -->
But the moment I add the cassandra dependancy, I start seeing this error:
***************************
APPLICATION FAILED TO START
***************************
Description:
The bean 'TestRepository', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.
Action:
Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true
For my debugging purpose, I have removed all the cassandra related classes(repository, entity, etc), so that cassandra jar is not being imported anywhere in the project. And hence, narrowed down to this as root cause.
I have no idea what is causing this issue. From the error log, it seems that the bean is trying to get created twice or so. But how come that is possible?
If I add the property, i run into a different issue which hints that cassandra import is trying to do something with the testRepository:
spring.main.allow-bean-definition-overriding=true
Error:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'testController': Unsatisfied dependency expressed through field 'TestRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'TestRepository': Cannot resolve reference to bean 'cassandraTemplate' while setting bean property 'cassandraTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cassandraTemplate' defined in class path resource [org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.class]: Unsatisfied dependency expressed through method 'cassandraTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'cassandraSession' defined in class path resource [org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.class]: Invocation of init method failed; nested exception is com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/0:0:0:0:0:0:0:1:9042 (com.datastax.driver.core.exceptions.TransportException: [localhost/0:0:0:0:0:0:0:1:9042] Cannot connect), localhost/127.0.0.1:9042 (com.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1:9042] Cannot connect))
Can someone please help on this. Thanks in advance.