Issue
I am new to using Spring Boot Applications. I would like to know how to connect cassandra database to the application. I added this dependency in the pom.xml file :
<dependency>
<groupId>com.datastax.oss</groupId>
<artifactId>java-driver-core</artifactId>
<version>4.1.0</version>
</dependency>
And these lines to the application.yml file :
spring:
data:
cassandra:
keyspace-name: test-keyspace
contact-points: localhost
port: 9042
schemaAction: CREATE_IF_NOT_EXISTS
request:
timeout: 10s
connection:
connect-timeout: 10s
locaDatacenter: datacenter1
However, when I run the application, I get such errors :
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name'org.springframework.boot.autoconfigure.data.cassandra.CassandraDataAutoConfiguration':
Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'cassandraSession' defined in class path resource [org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.class]:
Unsatisfied dependency expressed through method 'cassandraSession' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'cassandraSessionBuilder' defined in class path resource [org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.class]:
Unsatisfied dependency expressed through method 'cassandraSessionBuilder' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'cassandraDriverConfigLoader' defined in class path resource [org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.class]:
Bean instantiation via factory method failed;
nested exception is org.springframework.beans.BeanInstantiationException:
Failed to instantiate [com.datastax.oss.driver.api.core.config.DriverConfigLoader]: Factory method 'cassandraDriverConfigLoader' threw exception; nested exception is java.lang.NoSuchFieldError: CONNECTION_CONNECT_TIMEOUT
Solution
In addition to Aaron's answer, there's a good chance that there's a version conflict in your deployment that's leading to the exceptions you posted.
It isn't necessary to specify the Cassandra Java driver as a dependency because Spring Boot will pull all the required dependencies + correct versions automatically for you.
Since you're new, I would recommend that you start with a working example instead of doing one on your own. The Spring project has a repository on GitHub with Spring Data examples including one specific for Cassandra.
Additionally, DataStaxDevs recently ran an interactive workshop that shows you how to build a microservices app with Spring Data Cassandra and Spring Boot. Details of the workshop and the hands-on tutorials with full working code are available here -- https://github.com/DataStaxDevs/workshop-spring-data-cassandra. Cheers!
Answered By - Erick Ramirez
Answer Checked By - Gilberto Lyons (JavaFixing Admin)