I was able to make the following example project work on my system using HSQLDB and Jetty:
I then wanted to convert the project to use JNDI to reference the data source.
First, I added the following resource-ref to my web.xml
:
<resource-ref>
<res-ref-name>jdbc/mydatasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
I then added the following data source to my $JETTY_HOME/etc/jetty.xml
file:
<New id="mydatasource" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg></Arg>
<Arg>jdbc/mydatasource</Arg>
<Arg>
<New class="org.hsqldb.jdbc.JDBCDataSource">
<Set name="Url">file:mytestdb</Set>
<Set name="User">SA</Set>
<Set name="Password"></Set>
</New>
</Arg>
</New>
...and then I referenced the data source using the following code:
try {
//con=DriverManager.getConnection("jdbc:hsqldb:mydatabase","SA","");
InitialContext context = new InitialContext();
DataSource dataSource = (DataSource)context.lookup("java:comp/env/jdbc/mydatasource");
con = dataSource.getConnection();
con.createStatement().executeUpdate("create table contacts (name varchar(45),email varchar(45),phone varchar(45))");
} catch (NamingException ne) {
ne.printStackTrace(System.out);
} catch (SQLException e) {
e.printStackTrace(System.out);
}
I then deployed the app as an exploded directory and tried to start the Jetty process. When I did, it crashed and I got the following error message:
# tom at toshi in ~/Downloads/jetty-distribution-9.0.4.v20130625 [19:41:34]
$ java -jar ./start.jar <<<
2013-07-07 19:41:39.335:WARN:oejx.XmlConfiguration:main: Config error at <Set name="Password"/> java.lang.refle
ct.InvocationTargetException in file:/home/tom/Downloads/jetty-distribution-9.0.4.v20130625/etc/jetty.xml
2013-07-07 19:41:39.349:WARN:oejx.XmlConfiguration:main: Config error at <New id="mydatasource" class="org.ecli
pse.jetty.plus.jndi.Resource"><Arg/><Arg>jdbc/mydatasource</Arg><Arg>| <New class="org.hsqldb.jdbc.J
DBCDataSource"><Set name="Url">file:mytestdb</Set><Set name="User">SA</Set><Set name="Password"/></New>|
</Arg></New> java.lang.reflect.InvocationTargetException in file:/home/tom/Downloads/jetty-distribution-9.0.4.
v20130625/etc/jetty.xml
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.eclipse.jetty.start.Main.invokeMain(Main.java:509)
at org.eclipse.jetty.start.Main.start(Main.java:651)
at org.eclipse.jetty.start.Main.main(Main.java:99)
...
Ok, it doesn't like the fact that the password field is empty. So then I tried making this
change in my jetty.xml
:
<!--<Set name="Password"></Set>--> <!-- Got rid of this -->
<Set name="Password" />
I still got the same error. So I then deleted the "Password" property from my data source and I was able to start Jetty! The bad news is that now I get this error when I try to do anything that references the database:
2013-07-07 19:49:09.682:WARN:jndi:qtp14895676-19:
java.lang.Exception: org.hsqldb.jdbc.JDBCDataSource: invalid RefAddr: password
at org.hsqldb.jdbc.JDBCDataSourceFactory.getObjectInstance(Unknown Source)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:321)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:476)
at org.eclipse.jetty.jndi.local.localContextRoot.lookup(localContextRoot.java:518)
at org.eclipse.jetty.jndi.local.localContextRoot.lookup(localContextRoot.java:533)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:468)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:536)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:536)
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:411)
at Register.init(Register.java:31)
...
That warning then was followed by a null pointer exception.
So now it appears that the HSQLDB driver is counting on the existence of the password field in the data source.
So that's my catch-22: I'm not able to start Jetty with an empty password field in my data source, but if I omit it, the driver doesn't work.
Does anyone know of a way to create a data source with an empty password in Jetty?
Finally, I'm aware that I could a) not use JNDI or b) give the SA user a password. However, my particular project requires that I use JNDI and not give the SA user a password.