Search code examples
javaneo4jneo4j-ogm

java process won't stop after neo4j query


I'm connecting to a remote neo4j db using ConfigrationSource and SessionFactory from neo4j OGM like this:

private final static ConfigurationSource props = new ClasspathConfigurationSource("neo4j-connection.properties");
    private final static Configuration configuration = new Configuration.Builder(props).build();
    private final static SessionFactory sessionFactory = new SessionFactory(configuration, "domain");

Then I run a cypher query and get back an Iterable of POJO mapped to neo4j nodes with OGM annotations

private final LinkedBlockingQueue<Session> sessionPool = new LinkedBlockingQueue<>(8192);
@Override
    public Iterable<Location> findAll() {
        try {
            return session.loadAll(Location.class, 1);
        } finally {
            closeSession(session);
        }
    }
private void closeSession(Session session) {
        if (session != null) {
            try {
                sessionPool.offer(session, 100, TimeUnit.MILLISECONDS);
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }

Everything works fine, in my main method I have my POJO object created with neo4j values

public static void main(String[] args) {
        LocationServiceImpl locationService = new LocationServiceImpl();
        Iterable<Location> locations = locationService.findAll();
        for(Location location : locations) {
            System.out.println(location.getSubType());//eg. prints Floor, Building etc
        }
        System.out.println("End");
    }

But after the main method finishes the app won't stop. Above "End" string being printed. So some thread is hanging somewhere and I can't figure out where and why.

I tried closing the neo4j ogm session but with no luck.

My neo4j ogm pom dependencies

 <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-bolt-driver</artifactId>
            <version>3.1.2</version>
            <scope>runtime</scope>
        </dependency>
 <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-jdbc-driver</artifactId>
            <version>3.4.0</version>
        </dependency>
<dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-ogm-core</artifactId>
            <version>3.1.3</version>
        </dependency>

Print screen from the visual vm enter image description here


Solution

  • You have to close the SessionFactory as well.

    Some background: I assume that you are using the Bolt protocol for the connection. Creating an instance of SessionFactory will also create a java (bolt) driver instance with all its connection pooling etc. To have a clean shutdown call sessionFactory.close() that will also shutdown / close the driver.