3

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.

Somen Biswas
  • 103
  • 1
  • 1
  • 6
  • Does adding a `@Table` annotation to the `TestEntity` solve the problem? – Jens Schauder Jun 08 '20 at 06:33
  • @JensSchauder i have that already as: `import org.springframework.data.relational.core.mapping.Column;` `import org.springframework.data.relational.core.mapping.Table;` `@Table("test_tnt")` `public class TestEntity{}` – Somen Biswas Jun 08 '20 at 06:40
  • are you using the current version of Spring Data JDBC? There was a bug with this, but it is fixed for quite a while now. – Jens Schauder Jun 08 '20 at 06:43
  • I was using springboot 2.2.0.RELEASE. On upgrading to 2.2.6.RELEASE, it resolved one issue, but now i see this: 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; – Somen Biswas Jun 08 '20 at 07:03
  • nested exception is com.datastax.driver.core.exceptions.NoHostAvailableException: All host(s) tried for query failed (tried: localhost/127.0.0.1:9042 (com.datastax.driver.core.exceptions.TransportException: [localhost/127.0.0.1:9042] Cannot connect), 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)) – Somen Biswas Jun 08 '20 at 07:03
  • Hey. Are you sure your cassandra is up and running? Can you connect to it using cql client? – eladyanai Jun 08 '20 at 07:12
  • @eladyanai as mentioned in my post, I am not connecting to any cassandra db/table/repo. I start getting the error when I add spring-data-cassandra as dependancy. – Somen Biswas Jun 08 '20 at 12:34
  • Yes, because the way the cassandra java driver works is by connecting to the cassandra cluster on startup for node discovery, connection pool, etc. If you dont have a working cassandra it will fail! Check this out https://docs.datastax.com/en/developer/java-driver/3.0/ look on the troubleshooting section. – eladyanai Jun 08 '20 at 16:34
  • Thanks @eladyanai. I am able to make some progress on this, but I am running into another set of issue as mentioned in [https://stackoverflow.com/questions/62267799/spring-data-cassandra-error-creating-bean-with-name-sessionfactory-and-cannot]...I am new to using cassandra and spring-data-cassandra, so not sure where it is missing. – Somen Biswas Jun 08 '20 at 17:28
  • I doubt it helps with the current issue, but make sure alle JDBC entities have a `@Table` annotation. This will mark them for pickup by Spring Data JDBC. This is necessary when you have two or more Spring Data modules. – Jens Schauder Jun 09 '20 at 06:41
  • Hey. I will post my comment as an answer for other people with the same issue. we will continue working on the rest of them in the other thread. thank you. – eladyanai Jun 09 '20 at 07:14
  • Try this @Configuration @EnableCassandraRepositories( basePackages = "com.base package") public class CassandraConfig extends AbstractCassandraConfiguration { // } – Lakshman Jun 09 '20 at 07:54

2 Answers2

0

When using the cassandra dependency you get the cassandra java driver.

The driver is trying to connect to Cassandra on startup for getting information like cluster nodes (auto discovery), connection pool, keep alive, hash key range per node and more.

As the error states, you fail to connect to the Cassandra DB on localhost:

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))

Try to connect to your Cassandra cluster using cql client:

cqlsh localhost -u cassandra -p cassandra

if it fails you Cassandra is probably offline and you need to start it:

sudo dse cassandra -R

for more information please look here: https://docs.datastax.com/en/developer/java-driver/3.0/#troubleshooting

eladyanai
  • 1,063
  • 2
  • 15
  • 34
0

Try this

@Configuration
@EnableCassandraRepositories(
  basePackages = "base package where you have kept your cassandra jpa")
public class CassandraConfig extends AbstractCassandraConfiguration {

}
Lakshman
  • 469
  • 5
  • 12