How to open/close drivers and sessions in Neo4J Node.Js driver?

1.3k Views Asked by At

I'm using Neo4J Javascript Driver to query database from my Node.Js app.

Suppose I use the following construct to send several queries to Neo4J (looping through them).

When do I need to close the session and when do I need to close the driver?

Shall I do it in the end of every cycle (as below) or after all the cycles are complete? If it's the latter, how would I do that?

And another question — do I really have to close the driver? What if my app runs continuously? What if it quits because of some error and restarts again?

var driver = neo4j.driver("bolt://localhost", neo4j.auth.basic("neo4j", "neo4j"));

var session = driver.session();

var transactionQueries = ['MATCH ...', 'MATCH ...'];

for (var key in transactionQueries) {
session
.run(transactionQueries[key])
.subscribe({
onNext: function (record) {
  console.log(record.get('name'));
},
onCompleted: function () {
  session.close();
},
onError: function (error) {
  console.log(error);
}
});
}


driver.close();
2

There are 2 best solutions below

0
Natam Oliveira On

In my opinion, I think you can keep the connection (driver) but the session should be opened and closed like the instructions on neo4j-javascript-driver

// Create a session to run Cypher statements in.
// Note: Always make sure to close sessions when you are done using them!
var session = driver.session()

session
  .run('MERGE (james:Person {name : $nameParam}) RETURN james.name AS name', {
    nameParam: 'James'
  })
  .then(result => {
    result.records.forEach(record => {
      console.log(record.get('name'))
    })
  })
  .catch(error => {
    console.log(error)
  })
  .then(() => session.close())

Other think that I suggest to you is see if one OGM help you to code better like neo4j-node-ogm

0
zirkelc On

Sessions are lightweight and should be opened on demand and closed after your query. Here's the documentation on driver.session():

Acquire a session to communicate with the database. The session will borrow connections from the underlying connection pool as required and should be considered lightweight and disposable. This comes with some responsibility - make sure you always call close when you are done using a session, and likewise, make sure you don't close your session before you are done using it. Once it is closed, the underlying connection will be released to the connection pool and made available for others to use.

The driver connects to Neo4j and handles a connection pool that will be shared by all sessions. How big this connection pool is and how long each connection stays alive, depends on the configuration when the driver was created. See this documentation on neo4j.driver() for the default values.

It is recommended to create one driver instance and hold onto it, because creating a connection is expensive and may take a few seconds to establish. If the driver is connected to Neo4j and it wasn't interrupted by a network error can be verified by driver.verifyConnectivity().

This post about Neo4j best practices has some more information on connection and session handling.