我是能夠使使用HSQLDB和Jetty我的系統上下面的示例項目工作:錯誤使用數據源時無需輸入密碼在碼頭9
我當時就想轉換項目以使用JNDI來引用數據源。 首先,我添加了以下資源引用到我的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>
我加入了以下數據源到我的$JETTY_HOME/etc/jetty.xml
文件:
<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>
...然後我引用的數據源使用以下代碼:
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);
}
然後,我將該應用程序部署爲爆炸目錄,並嘗試啓動Jetty進程。 當我做到了,它死機了,我得到了以下錯誤消息:
# 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)
...
好了,它不喜歡的事實,密碼字段爲空。於是我試着在我jetty.xml
使這個 變化:
<!--<Set name="Password"></Set>--> <!-- Got rid of this -->
<Set name="Password" />
我還是得到了同樣的錯誤。所以我然後從我的數據源 中刪除了「密碼」屬性,並且我能夠啓動Jetty!壞消息是,現在當我嘗試做 任何引用數據庫我得到這個錯誤:
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)
...
的警告,然後其次是一個空指針異常。
所以現在看起來HSQLDB驅動程序正在計算數據源中密碼 字段的存在。
這就是我的catch-22:我不能在我的 數據源中用空密碼字段啓動Jetty,但是如果我忽略它,驅動程序將不起作用。
有誰知道在Jetty中用空密碼創建數據源的方法嗎?
最後,我知道我可以a)不使用JNDI或b)給SA用戶一個密碼。但是,我的特殊項目 要求我使用JNDI,而不是給SA用戶一個密碼。
您必須設置HSQLDB JDBCDataSource的密碼。在這種情況下爲「」(零長度字符串)。看起來,JNDI在空的時候根本就沒有設置密碼。 – fredt
感謝Fredt,這解決了我的問題!我希望我能說這個評論解決了這個問題。 –
不客氣。請使用解決方案自己回答問題並接受它。 – fredt