【问题标题】:Jetty 8(Embedded) With HSQLDB DatasourceJetty 8(嵌入式)与 HSQLDB 数据源
【发布时间】:2012-12-25 13:48:18
【问题描述】:

我已经绝望地尝试了整整两天,通过 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.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?

非常感谢您的帮助。

【问题讨论】:

  • 你发现你的代码有什么问题了吗?

标签: jndi hsqldb embedded-jetty


【解决方案1】:

这不是一个完整的答案,但在您提供的第二个 wiki 链接中显而易见的是使用数据源。创建 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>     

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-23
    • 2014-01-11
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多