title: Running H2DB on Tomcat for Spark-Java Web App
body: <p>My Spark Java app runs perfectly fine on it's embedded Jetty server with all h2database connections working fine.</p>
  
  <p>I packaged a war to run it on tomcat 8. However, once I deploy the war to tomcat. The connection to h2databse fails!!</p>
  
  <p>How to fix this issue? This is my web.xml</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="3.0"
           xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"&gt;
      &lt;display-name&gt;Hello&lt;/display-name&gt;
  
      &lt;filter&gt;
          &lt;filter-name&gt;SparkFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;spark.servlet.SparkFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;applicationClass&lt;/param-name&gt;
              &lt;param-value&gt;com.aqueel.project.Main&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;SparkFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Here is my Main class. The code fails only when I open sql2o connection. Which means tomcat cannot establish the connection while jetty server can.</p>
  
  <pre><code>String dataSource = "jdbc:h2:~/app123.db";
  
      String conString = dataSource + ";INIT=RUNSCRIPT from 'classpath:db/init.sql'";
      Sql2o sql2o = new Sql2o(conString, "", "");
      FoodDao foodDao = new Sql2oFoodDao(sql2o);
      Connection con = sql2o.open();
      Gson gson = new Gson();
  
      //GET MENU (ALL) works
      get("/delectable/menu", "application/json", (req, res) -&gt; {
          res.status(200);
          return foodDao.findAll();
      }, gson::toJson);
  </code></pre>
  
  <p>Here is my build.gradle I used to package war file:</p>
  
  <pre><code>group 'com.aqueel.project'
  
  apply plugin: 'java'
  apply plugin: 'war'
  
  sourceCompatibility = 1.8
  
  repositories {
      mavenCentral()
  }
  
  dependencies {
  
      compile "com.sparkjava:spark-core:2.5"
      compile "com.h2database:h2:1.4.191"
      compile "org.sql2o:sql2o:1.5.4"
      compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.6.1'
      compile group: 'com.google.code.gson', name: 'gson', version: '2.3.1'
      testCompile group: 'junit', name: 'junit', version: '4.11'
  
  }
  </code></pre>