【问题标题】:Keep JDBC references outside web.xml to deploy in Jetty, like in tomcat将 JDBC 引用保留在 web.xml 之外以部署在 Jetty 中,就像在 tomcat 中一样
【发布时间】:2012-03-18 17:54:28
【问题描述】:

我尝试将一些服务从我的 Tomcat 服务器移动到 Jetty,只是为了进行一些比较。显然我不想更改我的服务,但我尝试了一些问题来使用 JDBC 部署它们。

我的服务都使用同一个数据库来访问数据,所以我编写了自己的库来提出我的请求。这些服务没有关于数据库的任何信息,他们只知道他们必须使用图书馆。在这个库中,我使用这种代码与数据库建立连接:

InitialContext ictx = new InitialContext();
Context envCtx = (Context) ictx.lookup("java:comp/env");
DataSource ds = (DataSource) envCtx.lookup("jdbc/mysql");

在 Tomcat 中,我的服务运行良好,只需在 context.xml 中添加一行:

<Resource auth="Container" driverClassName="com.mysql.jdbc.Driver" maxActive="100" maxIdle="30" maxWait="10000" name="jdbc/mysql" username="login" password="password" type="javax.sql.DataSource" url="jdbc:mysql://localhost:3306/mysql" />

所以我只想在 Jetty 中做同样的事情。我在 jetty.xml 中添加了以下块:

<New id="mysql" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg></Arg>
    <Arg>jdbc/mysql</Arg>
    <Arg>
        <New class="org.apache.commons.dbcp.BasicDataSource">
            <Set name="driverClassName">com.mysql.jdbc.Driver</Set>
            <Set name="url">jdbc:mysql://localhost:3306/mysql</Set>
            <Set name="username">login</Set>
            <Set name="password">password</Set>
        </New>
    </Arg>
</New>

服务器启动良好并且似乎工作正常,但是当我尝试访问我的服务时出现错误。在码头的手册中,我发现它明确写到我必须在 web.xml 中添加一些信息,例如:

<resource-ref>
    <description>My DataSource Reference</description>
    <res-ref-name>jdbc/DSTest</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

所以我想知道除了在我的所有 web.xml 服务文件中编写相同的行之外,是否还有其他解决方案?就像为我的所有服务器添加一个具有相同信息的通用 xml 文件?

【问题讨论】:

  • Jetty 7.5 : jetty-7.5.0.v20110725 2011 年 7 月 25 日

标签: mysql web-services tomcat jdbc jetty


【解决方案1】:

您可以将其添加到 webdefault.xml 中,作为处理 web.xml 的基础。

此文件通常位于发行版的 etc/webdefault.xml 中。

干杯

【讨论】:

  • 谢谢,这正是我想要的 :)
猜你喜欢
  • 1970-01-01
  • 2014-11-30
  • 2013-06-24
  • 2015-09-08
  • 2011-02-08
  • 1970-01-01
  • 1970-01-01
  • 2010-12-16
  • 2018-09-11
相关资源
最近更新 更多