cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Best way to use Neo4J driver sessions

Hello,

I have a SpringBoot application where I'm using the following dependency:

<dependency>
      <groupId>org.neo4j.driver</groupId>
      <artifactId>neo4j-java-driver-spring-boot-starter</artifactId>
      <version>1.7.5</version>
</dependency>

My application needs to support high level of concurrent access for reads and writes.

At the moment, each time I want to interact with the database I'm creating a new session and then executing multiple transaction on that session scope. In the end, the session is closed. I'm doing it like this:

try (Session session = driver.session()) {
        return session.readTransaction(tx -> {
          String query = ...
          StatementResult result = tx.run(query);
          ...
        });
}

However, I was wondering if this the best way to do it, or if I should avoid the creation of a session everytime an interaction with the database is done. Would not be better to create a session when the application starts reuse that same session over and over? Or have 1 session for reads and another for writes?

I read in the documentation that sessions are not thread-safe. Not even for reads?

Thanks for your help.

3 REPLIES 3

MuddyBootsCode
Graph Steward

The most common way I've seen is to have one session for reads and another for writes.

But in that case does it behave well if there are concurrent read or write requests?

I guess what I mean to say is that unless you're using it without some sort of streaming tool or something like reddis, etc. The you're going to be calling functions that preform their own read and write actions and their own individual sessions. There's no leaving a session open otherwise, at least not in my experience. Some other folks on here may/hopefully have a more complete answer.