Search code examples
javaspringsessionneo4jneo4j-ogm

Is there an API to close Session in Neo4j-ogm?


I am using neo4j-ogm 1.1.4 version. Since I use the org.springframework.data.neo4j.template.Neo4jTemplate which I create by myself using the session object, I am wondering is there a contract that once all my work is done, then I have to mark the session closed.

I came across this link

http://inner-loop.github.io/java-neo4j-ogm/

But the library I am using seems to have no close method on the Session class. Is there any other API I need to use to mark the session closed?


Solution

  • There is technically no need to "close" the Session in the Neo4j OGM. It does not represent a connection to the database, instead it maintains conversational state between your application and the database, allowing the OGM to generate efficient Cypher when you load and save objects within a "unit of work" (as defined by your application).

    There are two ways to destroy this conversational state. They both have the same effect from the perspective of your application code.

    reuse

    session.clear() allows you to reuse the existing session object by deleting the existing conversational state.

    replace

    session = sessionFactory.openSession() will replace any current session object with a new one.

    Both these operations will leave the OGM with no information about the synchronisation state of your domain objects vis-a-vis the graph. (In Hibernate terms, they are in a 'detached' state) The OGM currently has no mechanism to re-attach your domain objects to a new session, so you should always reload all the objects you want to use into the new session.