我已经绝望地尝试了整整两天,通过 JNDI 通过 Jetty (8.1.8.v20121106) 公开使用 HSQLDB 的数据源。那里的大多数教程都已过时:例如http://dinukaroshan.blogspot.com/2012/04/setting-up-jndi-with-jetty-embedded.html使用 Jetty 6,从包中可以明显看出使用的名称(Mortbay 而不是 eclipse),或者他们只专注于jetty.xml
和jetty-env.xml
文件,而没有提供任何 Java 代码示例来说明他们如何使用这样的配置文件:
http ://wiki.eclipse.org/Jetty/功能/JNDI#Example_Webapps
我最好的尝试给了我以下结果:
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
我的jetty.xml
文件如下:
<?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>
我在我的web.xml
(这恰好是一个axis2 Web应用程序)中添加了以下内容:
<resource-ref>
<res-ref-name>jdbc/MySqlDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
这是我用来使用 Jetty 配置的 Java 代码:
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);
}
我究竟做错了什么?是否有最新的教程允许我使用 HSQLDB 作为我的 DBMS、JNDI 来公开数据源和 Jetty8?
非常感谢您的帮助。