title: Timer causing issues on Tomcat server
body: <p>I want to run a method every 22 minutes on a servlet running on Tomcat. So I'm using this code:</p>
  
  <pre><code>Timer timer = new Timer();
  
  timer.schedule(new TimerTask() {
      @Override
      public void run() {
  
          try {
              update();
          } catch (SQLException | NamingException | InterruptedException e1) {
              e1.printStackTrace();
          }
      }
  }, 22 * 60 * 1000, 22 * 60 * 1000);
  </code></pre>
  
  <p>I've got a feeling this is a terrible way to do it as I'm getting errors all the time regarding timers, and whenever I uploaded a new version of the servlet I don't think it stops the previous timer. I then get database connection warnings. If I reset everything and start fresh its fine. </p>
  
  <pre><code>javax.naming.NameNotFoundException: Name comp is not bound in this Context
      at org.apache.naming.NamingContext.lookup(NamingContext.java:770)
      at org.apache.naming.NamingContext.lookup(NamingContext.java:153)
      at org.apache.naming.SelectorContext.lookup(SelectorContext.java:152)
      at javax.naming.InitialContext.lookup(InitialContext.java:411)
      at my.app.database.DatabaseManager.connect(DatabaseManager.java:44)
      at my.app.database.DatabaseManager.returnInfo(DatabaseManager.java:133)
      at my.app.genParse.Generate.updateHistory(Generate.java:89)
      at my.app.MyServer$1.run(MyServer.java:52)
      at java.util.TimerThread.mainLoop(Timer.java:555)
      at java.util.TimerThread.run(Timer.java:505)
  </code></pre>
  
  <p>And also on re-launch with the new version:</p>
  
  <pre><code>SEVERE: The web application [/myApp] appears to have started a thread named [Timer-7] but has failed to stop it. This is very likely to create a memory leak.
  </code></pre>
  
  <p>Whats a better way to achieve or avoid this? </p>