After some years without using Neo4j I had the chance to use the graph database in my current project. Neo4j was a good fit because it makes it really easy to prototype an idea and the project itself relies heavily upon tree structures. Modelling tree structures in relational database management systems is always a PITA and takes a certain time to implement in the backend. The good news is that trees are only directed graphs which can be easily modelled with Neo4j.
As a Spring guy I used the latest spring-data-neo4j 4.1.x release to connect the application to the graph database. For easier prototyping I used the embedded driver.
import org.neo4j.ogm.session.SessionFactory; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.neo4j.repository.config.EnableNeo4jRepositories; import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableTransactionManagement @EnableNeo4jRepositories(basePackages = "de.schakko.neo4j") @Configuration public class Neo4jConfiguration extends org.springframework.data.neo4j.config.Neo4jConfiguration { @Override public SessionFactory getSessionFactory() { return new SessionFactory(getConfiguration(), "de.schakko.neo4j"); } @Bean public org.neo4j.ogm.config.Configuration getConfiguration() { org.neo4j.ogm.config.Configuration config = new org.neo4j.ogm.config.Configuration(); config.driverConfiguration() .setDriverClassName("org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver") .setURI("file:///var/tmp/graph.db"); return config; } }
When running the integration tests (or after automatic reloading of the application by spring-boot-devtools) the application failed to reconnect to the database:
org.neo4j.kernel.StoreLockException: Unable to obtain lock on store lock file: file:///var/tmp/graph.db
In the end I fixed the error by using
import org.junit.After; import org.junit.runner.RunWith; import org.neo4j.ogm.drivers.embedded.driver.EmbeddedDriver; import org.neo4j.ogm.service.Components; class ItemRepositoryIT { @After public void tearDown() { ((EmbeddedDriver) Components.driver()).close(); } // ... }
in the integration tests.