title: Embedded databases in Jetty
body: <p>What I want is when Jetty starts, it creates a database which can be used by my webapps. My goal is to be able to deploy my webapps on every computer to display my application.</p>
  
  <p>I need a way to declare my HSQLDB database (I've SQL-files for all my databases to set up the structure and to fill it with datas) in the Jetty configuration. Those parameters just have to be set one time and won't change in the future.</p>
  
  <p>I feel like I've looked for it everywhere and tried everything but nothing wants to work :( I'm using Jetty 9 by the way.</p>
  
  <p>This is one of the option I've tried and which seems to be close to my solution to me. I added this code to jetty/etc/jetty.xml</p>
  
  <pre><code>&lt;New id="toto" class="org.eclipse.jetty.plus.jndi.Resource"&gt;
    &lt;Arg&gt;&lt;/Arg&gt;
    &lt;Arg&gt;jdbc/toto&lt;/Arg&gt;
    &lt;Arg&gt;
      &lt;New class="org.apache.commons.dbcp.BasicDataSource"&gt;
        &lt;Set name="DriverClassName"&gt;org.hsqldb.jdbc.jdbcDataSource&lt;/Set&gt;
        &lt;Set name="Url"&gt;jdbc:hsqldb:hsql://localhost:9015/toto&lt;/Set&gt;
        &lt;Set name="Username"&gt;toto&lt;/Set&gt;
        &lt;Set name="Password"&gt;toto&lt;/Set&gt;
      &lt;/New&gt;
    &lt;/Arg&gt;
  &lt;/New&gt;
  </code></pre>
  
  <p>and this one to jett/etc/webdefault.xml</p>
  
  <pre><code>&lt;resource-ref&gt;
    &lt;res-ref-name&gt;jdbc/toto&lt;/res-ref-name&gt;
    &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
    &lt;res-auth&gt;Container&lt;/res-auth&gt;
  &lt;/resource-ref&gt;
  </code></pre>
  
  <p>Merry Christmas to anyone who can help me :)</p>
  
  <p><strong>Edit 26/12/2013 :</strong>
  Another option I tried is to configure the database through spring in Eclipse. Each webapp matches a project (maven architecture) and use its own database. Thus, for one project I did this :</p>
  
  <p>*conf/common/resources/applicationContext.xml (Project)</p>
  
  <pre><code>&lt;bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"&gt;
    &lt;property name="driverClassName" value="org.hsqldb.jdbcDriver"/&gt;
    &lt;property name="url" value="jdbc:hsqldb:mem:toto"/&gt;
    &lt;property name="username" value="toto"/&gt;
    &lt;property name="password" value="toto"/&gt;
  &lt;/bean&gt;
  </code></pre>
  
  <p>*conf/dev/WEB-INF/web.xml (Project)</p>
  
  <pre><code>&lt;resource-ref&gt;
    &lt;res-ref-name&gt;jdbc/toto&lt;/res-ref-name&gt;
    &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
    &lt;res-auth&gt;Container&lt;/res-auth&gt;
  &lt;/resource-ref&gt;
  </code></pre>
  
  <p>*conf/dev/WEB-INF/jetty-web.xml (Project)</p>
  
  <pre><code>&lt;Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext"&gt;
    &lt;New id="square" class="org.eclipse.jetty.plus.jndi.Resource"&gt;
      &lt;Arg&gt;&lt;Ref id="wac" /&gt;&lt;/Arg&gt;
      &lt;Arg&gt;jdbc/square&lt;/Arg&gt;
      &lt;Arg&gt;
       &lt;New class="org.hsqldb.jdbc.jdbcDataSource"&gt;
         &lt;Set name="Database"&gt;file:square&lt;/Set&gt;
         &lt;Set name="User"&gt;${database.connection.username}&lt;/Set&gt;
         &lt;Set name="Password"&gt;${database.connection.password}&lt;/Set&gt;
        &lt;/New&gt;
      &lt;/Arg&gt;
    &lt;/New&gt;
  &lt;/Configure&gt;
  </code></pre>
  
  <p>*jetty/start.ini (Jetty) : Uncomment these lines</p>
  
  <pre><code>OPTIONS=jndi
  OPTIONS=plus
  etc/jetty-plus.xml
  </code></pre>
  
  <p>With all that, I get this exception :</p>
  
  <pre><code>java.lang.ExceptionInInitializerError
  at org.apache.jasper.servlet.JspServlet.init(JspServlet.java:159)
  at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:540)
  at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:349)
  at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:812)
  at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:288)
  at org.eclipse.jetty.webapp.WebAppContext.startContext(WebAppContext.java:1322)
  at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:732)
  at org.eclipse.jetty.webapp.WebAppContext.doStart(WebAppContext.java:490)
  at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:69)
  at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:118)
  at org.eclipse.jetty.util.component.ContainerLifeCycle.addBean(ContainerLifeCycle.java:282)
  at org.eclipse.jetty.util.component.ContainerLifeCycle.addBean(ContainerLifeCycle.java:214)
  at org.eclipse.jetty.util.component.ContainerLifeCycle.updateBeans(ContainerLifeCycle.java:764)
  at org.eclipse.jetty.server.handler.HandlerCollection.setHandlers(HandlerCollection.java:89)
  at org.eclipse.jetty.server.handler.ContextHandlerCollection.setHandlers(ContextHandlerCollection.java:145)
  at org.eclipse.jetty.server.handler.HandlerCollection.addHandler(HandlerCollection.java:155)
  at org.eclipse.jetty.deploy.bindings.StandardDeployer.processBinding(StandardDeployer.java:41)
  at org.eclipse.jetty.deploy.AppLifeCycle.runBindings(AppLifeCycle.java:186)
  at org.eclipse.jetty.deploy.DeploymentManager.requestAppGoal(DeploymentManager.java:495)
  at org.eclipse.jetty.deploy.DeploymentManager.addApp(DeploymentManager.java:146)
  at org.eclipse.jetty.deploy.providers.ScanningAppProvider.fileAdded(ScanningAppProvider.java:175)
  at org.eclipse.jetty.deploy.providers.ScanningAppProvider$1.fileAdded(ScanningAppProvider.java:64)
  at org.eclipse.jetty.util.Scanner.reportAddition(Scanner.java:605)
  at org.eclipse.jetty.util.Scanner.reportDifferences(Scanner.java:528)
  at org.eclipse.jetty.util.Scanner.scan(Scanner.java:391)
  at org.eclipse.jetty.util.Scanner$1.run(Scanner.java:329)
  at java.util.TimerThread.mainLoop(Timer.java:555)
  at java.util.TimerThread.run(Timer.java:505)
  </code></pre>
  
  <p><strong>EDIT 07/01/2014</strong></p>
  
  <p>I gave up on that path but I succeeded to do what I want here: <a href="https://stackoverflow.com/questions/20899608/connect-databases-to-hsqldb-server">Connect databases to HSQLDB Server</a></p>