12/6/10

Configuring HSQL datasources in jetty for j2ee web applications

First an entry has to be put in the WEB-INF/web.xml file


  <resource-ref>
    <res-ref-name>jdbc/mydb</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>

Then for jetty we can use a special config file, which is looked up in every j2ee application deployed in jetty
i.e. jetty-env.xml . We put this in WEB-INF/jetty-env.xml of the webapp. The content of files look like


<Configure id="wac" class="org.eclipse.jetty.webapp.WebAppContext">
<New id="mydb" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/mydb</Arg>
<Arg>
<New class="org.apache.commons.dbcp.BasicDataSource">
<Set name="driverClassName">org.hsqldb.jdbc.JDBCDriver</Set>
<Set name="url">jdbc:hsqldb:hsql://localhost/mydb</Set>
<Set name="username">SA</Set>
<Set name="password">PASSWORD</Set>
</New>
</Arg>
</New>
</Configure>

Please note that jetty is not a full j2ee container. We have to explicitly change the jetty configuration files in order to enable jndi lookups.

No comments: