Published 05 Oct, 2022

Java - how to use cassandra session for multiple threads?

Category Java
Modified : Nov 29, 2022
33

I am using cassandra as backend in web application(in java), where for each user need to create unique session or use single one as in below url?

I read session information in this link. https://docs.datastax.com/en/drivers/java/2.0/com/datastax/driver/core/Session.html

Its saying Session instances are thread-safe. Means one thread at a time can use this instance.

Also written that each session maintains multiple connections to the cluster nodes.

Question is

  1. if only one thread can use on session instance then what is use of creating multiple connection per session to the cluster nodes?
  2. Doesn't it slow downs multithread operations on session?

Answers

There are 1 suggested solutions here and each one has been listed below with a detailed description. The following topics have been covered briefly such as Session, Multithreading, Java, Cassandra. These have been categorized in sections for a clear and precise explanation.

26

You got it wrong, it is safe to share an instance with different threads:

A session holds connections to a Cassandra cluster, allowing it to be queried. Each session maintains multiple connections to the cluster nodes...

Session instances are thread-safe and usually a single instance is enough per application.

Thread-safe means that it is safe to share the instance with other threads. It does not mean that you can only use it from a single one.