Jetty 8(嵌入式)与HSQLDB数据源

时间:2012-12-25 13:48:19

标签: jndi hsqldb embedded-jetty

通过Jetty(8.1.8.v20121106),我已经无缘无故地尝试通过JNDI使用HSQLDB公开数据源两整天。大多数教程都已过时:例如http://dinukaroshan.blogspot.com/2012/04/setting-up-jndi-with-jetty-embedded.html使用Jetty 6,这从使用的包名称(Mortbay而不是eclipse)中显而易见,或者他们只关注jetty.xmljetty-env.xml文件,没有提供任何Java代码示例,他们如何使用这样的配置文件: http://wiki.eclipse.org/Jetty/Feature/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?

非常感谢你的帮助。

1 个答案:

答案 0 :(得分:2)

这不是一个完整的答案,但在您提供的第二个wiki链接中显而易见的是使用DataSource。创建了一个javax.sql.DataSource实例。 HSQLDB的设置是这样的,当在给定示例中使用它而不是Derby时:

<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>