Reputation: 1066
I've been hopelessly trying for two complete days to expose a data source using HSQLDB through JNDI, through Jetty (8.1.8.v20121106). Most of the tutorials out there are out of date: For example http://dinukaroshan.blogspot.com/2012/04/setting-up-jndi-with-jetty-embedded.html uses Jetty 6, which is obvious from the package names that are used (Mortbay instead of eclipse), or they just concentrate on the jetty.xml
and jetty-env.xml
files, without giving any Java code samples on how did they consume the configuration files like this one:
http://wiki.eclipse.org/Jetty/Feature/JNDI#Example_Webapps
My best attempt at doing it has given me the following result:
java.lang.Exception: javax.naming.NameNotFoundException; remaining name 'env/jdbc/MySqlDS'
at wavemark.dcpcontroller.controllerws.db.DBProxy.getConnection(DBProxy.java:47)
at test.wavemark.dcpcontroller.controllerws.webservice.TestDCPControllerWS.getConnection(TestDCPControllerWS.java:97)
at org.dbunit.DatabaseTestCase.newDatabaseTester(DatabaseTestCase.java:85)
at org.dbunit.DatabaseTestCase.getDatabaseTester(DatabaseTestCase.java:109)
at org.dbunit.DatabaseTestCase.tearDown(DatabaseTestCase.java:164)
at test.wavemark.dcpcontroller.controllerws.webservice.TestDCPControllerWS.setUp(TestDCPControllerWS.java:33)
at junit.framework.TestCase.runBare(TestCase.java:132)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javax.naming.NameNotFoundException; remaining name 'env/jdbc/MySqlDS'
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:505)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:536)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:551)
at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:117)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at wavemark.dcpcontroller.controllerws.db.DBProxy.getConnection(DBProxy.java:43)
... 19 more
My jetty.xml
file is as follows:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure id='wac' class="org.eclipse.jetty.server.Server">
<!-- ============================================================== -->
<!-- Add the DataSource(s) only valid for this webapp below -->
<!-- ============================================================== -->
<New id="MySqlDS" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/MySqlDS</Arg>
<Arg>
<Set name="driverClass">org.hsqldb.jdbcDriver</Set>
<!--<Set name="url">jdbc:hsqldb:sample</Set>-->
<Set name="user">sa</Set>
<Set name="password"></Set>
</Arg>
</New>
</Configure>
I added the following to my web.xml
(This happens to be an axis2 web application):
<resource-ref>
<res-ref-name>jdbc/MySqlDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
And this is the Java code I'm using to consume the Jetty configuration:
public ServerManager(int webServerPort,String jettyConfigurationsFilePath, String webApplicationPath) throws Exception
{
System.out.println("Initializing server");
this.webServerPort = webServerPort;
this.webApplicationPath = webApplicationPath;
server = new Server(webServerPort);
XmlConfiguration configuration = new XmlConfiguration(new File(jettyConfigurationsFilePath).toURI().toURL());
configuration.configure(server);
System.out.println("Finished configuring Jetty Server...");
System.out.println("Server initialized");
System.out.println("Server state is now " + server.getState());
WebAppContext webapp = new WebAppContext();
webapp.setContextPath("/");
webapp.setWar(webApplicationPath);
server.setHandler(webapp);
}
What am I doing wrong? Is there an up-to-date tutorial that would allow me to use HSQLDB as my DBMS, JNDI to expose the data source, and Jetty8?
Thanks a lot for the help.
Upvotes: 0
Views: 3662
Reputation: 24382
This is not a complete answer, but something obvious in the second wiki link you provide is the use of a DataSource. An instance of javax.sql.DataSource is created. The settings for HSQLDB are like this, when it is used instead of Derby in the given example:
<Configure id='wac' class="org.eclipse.jetty.webapp.WebAppContext">
...
<New id="myds" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref id="wac"/></Arg>
<Arg>jdbc/mydatasource</Arg>
<Arg>
<New class="org.hsqldb.jdbc.JDBCDataSource">
<Set name="DatabaseName">file:mytestdb</Set>
<Set name="User">SA</Set>
<Set name="Password"></Set>
</New>
</Arg>
</New>
</Configure>
Upvotes: 2