title: Jersey web app with embedded Neo4J doesn't shut down properly on Tomcat server
body: <p>Preliminary: I looked for similar questions on SO and the one most closely resembling mine is this: <a href="https://stackoverflow.com/questions/24191950/unable-to-shut-down-neo4j-jetty-server-within-tomcat-webapp">Unable to shut down neo4j jetty server within tomcat</a>. I've tried all solutions proposed there but without success.</p>
  
  <p>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:</p>
  
  <pre>
  
      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]
  
  </pre>
  
  <p>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. </p>
  
  <p>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: <a href="https://github.com/marijnkoolen/java-jersey-neo4j-test" rel="nofollow noreferrer">https://github.com/marijnkoolen/java-jersey-neo4j-test</a> 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.</p>
  
  <p>For ease of reading, here are relevant code snippets.</p>
  
  <p>TestApplication.java:</p>
  
  <pre>
  
      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
          }
      }
  
  </pre>
  
  <p>web.xml:</p>
  
  <pre><code>&lt;!DOCTYPE web-app PUBLIC
   "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
   "http://java.sun.com/dtd/web-app_2_3.dtd" &gt;
  
  &lt;web-app&gt;
      &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;jersey-servlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.glassfish.jersey.servlet.ServletContainer&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;jersey.config.server.provider.packages&lt;/param-name&gt;
              &lt;param-value&gt;com.marijn.jerseytest&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;javax.ws.rs.Application&lt;/param-name&gt;
              &lt;param-value&gt;org.mytest.TestApplication&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;jersey-servlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>pom.xml:</p>
  
  <pre><code>&lt;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"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;org.mytest&lt;/groupId&gt;
      &lt;artifactId&gt;org.mytest&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;1.0-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;org.mytest Maven Webapp&lt;/name&gt;
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;se.jiderhamn&lt;/groupId&gt;
              &lt;artifactId&gt;classloader-leak-prevention&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
              &lt;version&gt;1.15.4&lt;/version&gt;
              &lt;exclusions&gt;
                  &lt;exclusion&gt;
                      &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
                      &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
                  &lt;/exclusion&gt;
              &lt;/exclusions&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
            &lt;groupId&gt;junit&lt;/groupId&gt;
            &lt;artifactId&gt;junit&lt;/artifactId&gt;
            &lt;version&gt;4.12&lt;/version&gt;
            &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.core&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-server&lt;/artifactId&gt;
              &lt;version&gt;2.22.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.containers&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-container-servlet-core&lt;/artifactId&gt;
              &lt;version&gt;2.22.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.glassfish.jersey.test-framework.providers&lt;/groupId&gt;
              &lt;artifactId&gt;jersey-test-framework-provider-jdk-http&lt;/artifactId&gt;
              &lt;version&gt;2.22.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.ws.rs&lt;/groupId&gt;
              &lt;artifactId&gt;javax.ws.rs-api&lt;/artifactId&gt;
              &lt;version&gt;2.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.neo4j&lt;/groupId&gt;
              &lt;artifactId&gt;neo4j&lt;/artifactId&gt;
              &lt;version&gt;3.1.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.neo4j&lt;/groupId&gt;
              &lt;artifactId&gt;neo4j-io&lt;/artifactId&gt;
              &lt;version&gt;3.1.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.neo4j&lt;/groupId&gt;
              &lt;artifactId&gt;neo4j-kernel&lt;/artifactId&gt;
              &lt;version&gt;3.1.1&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.neo4j&lt;/groupId&gt;
              &lt;artifactId&gt;neo4j-shell&lt;/artifactId&gt;
              &lt;version&gt;3.1.1&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
      &lt;build&gt;
          &lt;finalName&gt;org.mytest&lt;/finalName&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.apache.tomcat.maven&lt;/groupId&gt;
                  &lt;artifactId&gt;tomcat7-maven-plugin&lt;/artifactId&gt;
                  &lt;version&gt;2.2&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;url&gt;http://localhost:8080/manager/text&lt;/url&gt;
                      &lt;server&gt;UnixTomcatServer&lt;/server&gt;
                      &lt;path&gt;/mytest&lt;/path&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>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 (<a href="https://github.com/Neo4j/Neo4j/issues/1070" rel="nofollow noreferrer">https://github.com/Neo4j/Neo4j/issues/1070</a>) which kills many threads and takes care of the ThreadLocal errors, but not the thread pool issue.</p>
  
  <p>Setup:</p>
  
  <ul>
  <li>Java version: 1.8.0_121</li>
  <li>Maven version: 3.3.9</li>
  </ul>
  
  <p>Tested in the original application with:</p>
  
  <ul>
  <li>tomcat versions 6, 8 and 9</li>
  <li>Neo4j versions 2.2.3, 2.2.5 and 3.1</li>
  <li>Jersey versions 1.19 and 2.22.1</li>
  </ul>
  
  <p>In the simple app reported here:</p>
  
  <ul>
  <li>Tomcat 8 and 9</li>
  <li>Neo4j 3.1</li>
  <li>Jersey 2.22.1</li>
  </ul>
  
  <p>Any help would be appreciated. </p>
  
  <p>Many thanks,</p>
  
  <p>Marijn</p>
  
  <p><strong>Update 2017-03-02</strong>
  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.</p>