Hot questions for Using Neo4j in tomcat

Question:

Preliminary: I looked for similar questions on SO and the one most closely resembling mine is this: Unable to shut down neo4j jetty server within tomcat. I've tried all solutions proposed there but without success.

I'm running into a shutdown issue when running a Jersey application with embedded Neo4j database on Tomcat. Deploying (using Maven 3.3.9) works fine and the database is started, but on redeploy/undeploy, I get a Tomcat warning:


    21-Feb-2017 17:37:10.242 WARNING [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.clearReferencesThreads The web application [mytest] appears to have started a thread named [pool-17-thread-1] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
     sun.misc.Unsafe.park(Native Method)
     java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:215)
     java.util.concurrent.SynchronousQueue$TransferStack.awaitFulfill(SynchronousQueue.java:460)
     java.util.concurrent.SynchronousQueue$TransferStack.transfer(SynchronousQueue.java:362)
     java.util.concurrent.SynchronousQueue.poll(SynchronousQueue.java:941)
     java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1066)
     java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1127)
     java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
     java.lang.Thread.run(Thread.java:745)
    21-Feb-2017 17:37:10.242 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@2c930a78]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@4c0b4554]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.242 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@1c3e6e38]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@3f71b23c]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.242 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@b05bf60]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@1d326ab1]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.242 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@72929881]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@40c024bd]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.242 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@66987d7a]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@68f1f4fa]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.242 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@e413ff5]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@4f65074c]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.242 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@6ab3d077]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@d02bfff]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.242 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@a3b973d]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@6d03be51]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@1947fbee]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@1a2527e]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@5776bf08]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@27776281]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@149750a]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@12ebf6ff]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@70078555]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@6cf882be]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@26d35b0e]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@6bc20d25]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@27a30025]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@f669b1d]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@33d1bad4]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@480122ae]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@779cb1a3]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@4c19623e]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@72206673]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@33f53ecb]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@21f680bf]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@26f91e2d]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@571bf77f]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@4d828831]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@58e40a46]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@36026f01]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.243 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@3eaac6b1]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@67300255]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.244 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@1f7e82cf]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@3a218c46]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.244 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@52d64afd]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@3513d858]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.244 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@5e104ccf]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@68e5b8f7]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.244 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@80486b4]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@237e5ef4]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.244 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@7fd41860]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@3a33256d]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.244 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@16ae35b9]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@47f65066]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.244 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@18be57e0]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@3b0501d7]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.244 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.io.pagecache.impl.muninn.CursorPool] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool@5ce1e8f3]) and a value of type [org.neo4j.io.pagecache.impl.muninn.CursorPool.CursorSets] (value [org.neo4j.io.pagecache.impl.muninn.CursorPool$CursorSets@2961f669]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.244 SEVERE [http-nio-8080-exec-6] org.apache.catalina.loader.WebappClassLoaderBase.checkThreadLocalMapForLeaks The web application [mytest] created a ThreadLocal with key of type [org.neo4j.collection.pool.MarshlandPool$1] (value [org.neo4j.collection.pool.MarshlandPool$1@438ab5f2]) and a value of type [org.neo4j.collection.pool.MarshlandPool.LocalSlot] (value [org.neo4j.collection.pool.MarshlandPool$LocalSlot@50120fed]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
    21-Feb-2017 17:37:10.762 INFO [http-nio-8080-exec-6] org.apache.catalina.startup.HostConfig.undeploy Undeploying context [/mytest]

I've read that this warning is not necessarily an issue, but for my application this causes a real problem, because I need to access the embedded Neo4j DB via remote shell (I'm using port 5555). If I configure the embedded database to enable remote shell on port 5555, then upon shutdown the thread pool error causes the connection to port 5555 to remain active. I can only redeploy by shutting down tomcat and restarting.

I've created a simple version that only starts and stops a Neo4j embedded database, that shows the same problem (to ensure it's not a hanging DB transaction) and put the code on GitHub: https://github.com/marijnkoolen/java-jersey-neo4j-test in case someone wants to check if they can replicate this. There are no thread pool issues if I don't start an embedded database, so the problem arises in the interaction between Jersey and Neo4J. I'm not very experienced with Jersey and embedded databases, so it's very well possible I'm doing something stupid.

For ease of reading, here are relevant code snippets.

TestApplication.java:


    package org.mytest;

    import javax.ws.rs.core.Application;
    import javax.annotation.PostConstruct;
    import javax.annotation.PreDestroy;
    import java.util.HashSet;
    import java.util.Set;

    import java.io.File;

    import org.neo4j.graphdb.GraphDatabaseService;
    import org.neo4j.graphdb.factory.GraphDatabaseFactory;
    import org.neo4j.shell.shellSettings;

    /**
     * Created by marijn on 16-2-17.
     */
    public class TestApplication extends Application {

        File db_location = new File("/data/tagaid/neo4jdb");
        GraphDatabaseService db;

        public Set> getClasses() {
            Set> s = new HashSet>();
            s.add(HelloWorldService.class);
            return s;
        }


        @PostConstruct
        public void initialize() {
            try {
                db = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(db_location)
                        .setConfig(shellSettings.remote_shell_enabled, "true")
                        .setConfig(shellSettings.remote_shell_port, 5555)
                        .newGraphDatabase();
                System.out.println("Neo4j startup succeeded"); // logged in tomcat catalina.out
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Neo4j startup failed"); // logged in tomcat catalina.out
            }
        }

        @PreDestroy
        public void cleanUp() {
            try {
                db.shutdown();
                System.out.println("Neo4j shutdown succeeded"); // logged in tomcat catalina.out
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("Neo4j shutdown failed!"); // logged in tomcat catalina.out
            }
            db = null; // should not be necessary
        }
    }

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
    <display-name>Archetype Created Web Application</display-name>
    <servlet>
        <servlet-name>jersey-servlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.marijn.jerseytest</param-value>
        </init-param>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>org.mytest.TestApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.mytest</groupId>
    <artifactId>org.mytest</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>org.mytest Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>se.jiderhamn</groupId>
            <artifactId>classloader-leak-prevention</artifactId>
            <scope>runtime</scope>
            <version>1.15.4</version>
            <exclusions>
                <exclusion>
                    <artifactId>servlet-api</artifactId>
                    <groupId>javax.servlet</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.12</version>
          <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>2.22.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet-core</artifactId>
            <version>2.22.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.test-framework.providers</groupId>
            <artifactId>jersey-test-framework-provider-jdk-http</artifactId>
            <version>2.22.1</version>
        </dependency>
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-io</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-kernel</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>org.neo4j</groupId>
            <artifactId>neo4j-shell</artifactId>
            <version>3.1.1</version>
        </dependency>
    </dependencies>
    <build>
        <finalName>org.mytest</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <url>http://localhost:8080/manager/text</url>
                    <server>UnixTomcatServer</server>
                    <path>/mytest</path>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

In the catalina.out log I can see that the @PostConstruct and @PreDestroy functions are called and the shutdown is executed. Then the thread pool warning is logged. I've tried the JVMHelper that was suggested for a similar issue (https://github.com/Neo4j/Neo4j/issues/1070) which kills many threads and takes care of the ThreadLocal errors, but not the thread pool issue.

Setup:

  • Java version: 1.8.0_121
  • Maven version: 3.3.9

Tested in the original application with:

  • tomcat versions 6, 8 and 9
  • Neo4j versions 2.2.3, 2.2.5 and 3.1
  • Jersey versions 1.19 and 2.22.1

In the simple app reported here:

  • Tomcat 8 and 9
  • Neo4j 3.1
  • Jersey 2.22.1

Any help would be appreciated.

Many thanks,

Marijn

Update 2017-03-02 I've updated to Tomcat 9 and added the remote shell connection to the application code above. In the GitHub repo I made I've added a simple unit test as suggested by @jerome-b to show that the remote shell port is not released upon shutdown.


Answer:

I filed an issue with Neo4J (#8929). This is a bug (port remains bound until JVM terminates), with a fix proposed in issue #8949.

Question:

I am using the jdbc bolt library for connecting to a Neo4J database in Java. My code runs perfectly in main method, but after I switch it to servlet it gives error No suitable driver found for 'jdbc:neo4j:bolt://localhost'. What should I do?

//Test class

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class Test {
    public void test() {
        try {
            Connection con = DriverManager.getConnection("jdbc:neo4j:bolt://localhost", "neo4j", "neo4j");
            String query = "MATCH (n) return id(n)";
            PreparedStatement ps = con.prepareStatement(query);
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                System.out.println("RESULT: " + rs.getObject("id(n)"));
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


//Servlet

import java.io.IOException;
   import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/testingservlet")
public class testingservlet extends HttpServlet {
private static final long serialVersionUID = 1L;

    public testingservlet() {
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            new Test().test();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}

Answer:

Use official java library for neo4j database connectivity.

Question:

I'm working on an webserver project with tomact. I have a dataBase in neo4j. How can I link it to the dataBase? I want to get information from there and also add new data, in the servlets that I created. I tried running the dataBase in the background But i still get "Forbidden (403) - Forbidden" in my project. Do I have to set it somehow? I'm using eclipse, how can I do that?


Answer:

You can try this approach:

import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Transaction;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;

// ...

public static void main(String[] args) {
        GraphDatabaseFactory graphDbFactory = new GraphDatabaseFactory();
        GraphDatabaseService graphDb = graphDbFactory.newEmbeddedDatabase("path_to_neo4j_database/your_database_folder");
        try (Transaction tx = graphDb.beginTx()) {
            // use graphDb object to manipulate the DB content
            tx.success();
        }
        graphDb.shutdown();
        System.out.println("Done :) ");
}
// ...

And don't forget to add the Neo4j library to build path:

Question:

Hi i'm deploying an app that i wrote on a machine (all deployment with tomcat). when running the application (in browser) i get the error taken out from catalina.out :

java.lang.RuntimeException: Error starting org.neo4j.kernel.impl.factory.CommunityFacadeFactory,

/home/ubu01/neo4jDB at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.newFacade(GraphDatabaseFacadeFactory.java:143) at org.neo4j.kernel.impl.factory.CommunityFacadeFactory.newFacade(CommunityFacadeFactory.java:43) at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.newFacade(GraphDatabaseFacadeFactory.java:108) at org.neo4j.graphdb.factory.GraphDatabaseFactory.newDatabase(GraphDatabaseFactory.java:129) at org.neo4j.graphdb.factory.GraphDatabaseFactory$1.newDatabase(GraphDatabaseFactory.java:117) at org.neo4j.graphdb.factory.GraphDatabaseBuilder.newGraphDatabase(GraphDatabaseBuilder.java:185) at com.saeedh.utils.NeoQuery.InitDatabase(NeoQuery.java:46) at com.saeedh.utils.NeoQuery.AuthinUser(NeoQuery.java:153) at com.saeedh.servlets.start.Login.doGet(Login.java:79) at javax.servlet.http.HttpServlet.service(HttpServlet.java:622) at javax.servlet.http.HttpServlet.service(HttpServlet.java:729) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at com.saeedh.filters.PageValidationFilter.doFilter(PageValidationFilter.java:48) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:521) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) at java.lang.Thread.run(Thread.java:745) Caused by: org.neo4j.kernel.lifecycle.LifecycleException: Component 'org.neo4j.kernel.NeoStoreDataSource@4724a951' was successfully initialized, but failed to start. Please see attached cause exception. at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:462) at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:111) at org.neo4j.kernel.impl.transaction.state.DataSourceManager.start(DataSourceManager.java:112) at org.neo4j.kernel.lifecycle.LifeSupport$LifecycleInstance.start(LifeSupport.java:452) at org.neo4j.kernel.lifecycle.LifeSupport.start(LifeSupport.java:111) at org.neo4j.kernel.impl.factory.GraphDatabaseFacadeFactory.newFacade(GraphDatabaseFacadeFactory.java:139)

but the application works fine on my machine . i tried the solution suggested here : Error Obtaining Lock (org.neo4j.kernal.StoreLockException)

it didn't work , i am accessing the DB using :

db = new GraphDatabaseFactory().newEmbeddedDatabaseBuilder(new File("path to the database")).loadPropertiesFromFile("path to properties file")
        .newGraphDatabase();
registerShutdownHook(db);

what could be the problem ?


Answer:

Only one process at a time can access the store directory on disk.

As you start an embedded database, make sure that all servers and other processes accessing that same database are stopped cleanly.