Create a Neo4J DAO using Spring Data Neo4j

https://www.baeldung.com/spring-data-neo4j-intro explains how to configure Spring with Neo4J and use repositories to get data.

https://neo4j.com/docs/ogm-manual/current/tutorial/#tutorial-session shows how to create a CRUD service using the Session object.

As written in the Session Javadoc, Session object is not thread safe.

session-not-thread-safe

 

Most of our DAO objects are singletons used by many threads. So the Session class member should be replaced by a SessionFactory which will create a new Session each time we need it.

first-version

The Service calling the DAO is annotated with @Transactional to have transaction support on all his methods.

service

Running this code causes a org.neo4j.ogm.exception.core.TransactionManagerException: Transaction is not current for this threadtransactionmanagerexception

The code of Neo4jTransactionManager shows that a new Session is opened when starting a transaction.neo4j-dao-begin-transaction

And it is stored in the TransactionSynchronizationManager, a « thread local holder » that is cleared at the end of the transaction.

session-holder

The created Session can easily be retrieved through the TransactionSynchronizationManager.

fina-get-session

The following DAO work fine if we are in a Spring-managed transactional context

neo4j-dao

GC Overhead with Soap-UI, Maven and jUnit

On a project I decided to use Soap-UI to test REST services. Since I wanted to add some DBUnit datasets before executing the test, I decided to use it with jUnit as described here.

At the beginning everything worked well but as more tests were added, a GC Overhead appears and increasing the Heap Space and the Permgen did not resolved anything.

I will now tell you how this GC Overhead was resolved.

When a GC Overhead or any memory problem occurs the execution must be monitored with jvisualvm a tool shipped with the JDK

image003.jpg

It appears that the memory increase along the build without been released (memory leak).

image007.png

To analyze the memory, a dump must be fetched. This can easily be done in jvisualvm by clicking on Heap Dump.

image006.jpg

Memory Analyzer Tool is a tool that analyze the memory. It can find memory leaks.

When opening the dump, Memory Analyzer Tool find immediately the problem

image008.png

By clicking on details, we can see what happens

image009.png

The object InferredSchemaManager from Soap-UI has a Vector containing 2024 objects that occupies more than 1 GB of memory. It is a very good candidate for memory leak.

To see when this Vector is filled, we must have a look at the code of Soap-UI. It can be easily found on gitHub repository https://github.com/SmartBear/soapui

By opening InferredSchemaManager, we can see that there are static collections : perfect candidates for memory leaks because they stay alive as long as the application runs.

image011.png

And we see that it is filled each time we load the schema of a rest service

image015.png

By looking at what the utility of schemas map, we see that it is just a cache for loading schemas so empty it will just have performances issues.

Luckily, there is a release method that empty the table

release.png

By watching the call hierarchy we can see that this method is called by WsdlProject.release().

image017.jpg

image019.png

By watching what this method does we see that it releases project resources. So we can call it at the end of our tests.

release.png

And we see that we don’t have neither memory leaks nor GC Overhead anymore.

image021.jpg

It’s weird because this method is never mentioned in the documentation.

image022.png