4
votes

Thinking about Cassandra's Session and Cluster classes (Java driver) I wonder whats the difference. In Hibernate one creates a session every time and shares the session factory.

From many sources I learned that it is considered to create a single session and share it across many threads.

I do find this a bit curios. I would have expected that I create a session for each thread or ongoing action / task.

  • Do I have issues when doing so?
  • Whats the performance impact and is session thread-safe?
  • Do I run into cursor problems on larger result-sets (or does cassandra has no cursors?)
  • Is it safe to assume that Session is thread-safe or are there edge cases I should be aware of?
1

1 Answers

2
votes

In traditional RDBMS, a session usually refers to a transactional context and many implementations aren't thread safe. Meanwhile, the session is usually implemented on top of some kind of connection pooling and the termination of a connection (via error) also terminates the session.

In Cassandra, there isn't really a transactional context over multiple calls, so once connected to a keyspace, the session shareable and doesn't need to re-established. In addition, the driver already handles reconnection and connection pooling, so sessions aren't connection dependent.

Finally, statements prepared against a session are cached and it would be inefficient to re-create sessions and have to re-prepare statements constantly.