Jetty mysql 连接池配置错误:javax.naming.NameNotFoundException;剩余名称 'env/jdbc/---(mysql 5.0+jetty 7.0.1)

发布于 2024-08-18 21:36:39 字数 2007 浏览 5 评论 0原文

我的配置文件

project/WEB-INF/web.xml

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

project/WEB-INF/jetty-env.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/db</Set>
            <Set name="username">user</Set>
            <Set name="password">pwd</Set>
            <Set name="maxActive">50</Set>
        </New>
    </Arg>
</New>

要调用的代码:

ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
con=ds.getConnection();

启动jetty的脚本:

java -DOPTIONS=plus -jar start.jar

java -jar start.jar

无论哪种方式启动jetty,我都遵循以下内容错误:


javax.naming.NameNotFoundException; remaining name 'env/jdbc/mysql'
        at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:632)
        at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:663)
        at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:678)
        at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:110)
        at javax.naming.InitialContext.lookup(Unknown Source)

问题是:

  • 这里有什么问题?
  • 还需要其他配置吗?
  • 在哪里放置以下 jar 文件:
    • commons-dbcp-1.2.2.jar
    • mysql-connector-java-5.1.10-bin.jar

谢谢!

My configuration files

project/WEB-INF/web.xml:

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

project/WEB-INF/jetty-env.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/db</Set>
            <Set name="username">user</Set>
            <Set name="password">pwd</Set>
            <Set name="maxActive">50</Set>
        </New>
    </Arg>
</New>

code to invoke:

ctx = new InitialContext();
ds = (DataSource) ctx.lookup("java:comp/env/jdbc/mysql");
con=ds.getConnection();

scripts to start jetty:

java -DOPTIONS=plus -jar start.jar

java -jar start.jar

Either way to start jetty, I got following error:


javax.naming.NameNotFoundException; remaining name 'env/jdbc/mysql'
        at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:632)
        at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:663)
        at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:678)
        at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:110)
        at javax.naming.InitialContext.lookup(Unknown Source)

The questions are:

  • what is the problem here?
  • Any other configurations needed?
  • where to put following jar files:
    • commons-dbcp-1.2.2.jar
    • mysql-connector-java-5.1.10-bin.jar

Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

享受孤独 2024-08-25 21:36:39

注意:以下假设您使用的是 Jetty 7.2.2+(可能适用于任何 Jetty 7.0+)。

问题是您缺少一层额外的间接层。即使您已经在 Web 应用程序中配置了 Jetty,您仍然需要告诉 Jetty 容器它需要在您的 Web 应用程序中查找 jetty-env.xml。为此,请将以下内容添加到您的 jetty.xml 中(在 ${JETTY_INSTALL_DIR}/etc 下):

<Call name="setAttribute">
  <Arg>org.eclipse.jetty.webapp.configuration</Arg>
  <Arg>
      <Array type="java.lang.String">
          <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
          <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
          <Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.TagLibConfiguration</Item>
      </Array>
  </Arg>
</Call>

现在 Jetty 将知道如何解析您创建的 jetty-env.xml。

Note: The following assumes you're using Jetty 7.2.2+ (probably works with any Jetty 7.0+).

The problem is you're missing ONE extra layer of indirection. Even though you've configured Jetty in your webapp, you still need to tell the Jetty container that it needs to look for jetty-env.xml in your webapp. To do that, add the following to your jetty.xml (under ${JETTY_INSTALL_DIR}/etc:

<Call name="setAttribute">
  <Arg>org.eclipse.jetty.webapp.configuration</Arg>
  <Arg>
      <Array type="java.lang.String">
          <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
          <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
          <Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.TagLibConfiguration</Item>
      </Array>
  </Arg>
</Call>

NOW Jetty will know to parse the jetty-env.xml you've created.

﹏半生如梦愿梦如真 2024-08-25 21:36:39

这让我一整天都发疯。仅供参考,我正在使用 Jetty 6.1.22。第一篇文章的评论已步入正轨——我在 WEB-INF 中有一个文件名 jetty-env.xml,但它被忽略了。我将其重命名为 jetty-web.xml 并为 New 指令的第二个参数指定了数据源的全名(即 java:comp/env/jdbc/mydatasource),并且它起作用了。

This drove me nuts all day long. FYI, I am working with Jetty 6.1.22. The comment to the first post is on track -- I had a file name jetty-env.xml in WEB-INF, and it was being ignored. I renamed it to jetty-web.xml and specified the full name of the data source (i.e. java:comp/env/jdbc/mydatasource) for the second argument of the New directive and it worked.

岁月打碎记忆 2024-08-25 21:36:39

面临同样的问题。
我的 project/WEB-INF/jetty-env.xml 是:

<New id="DS" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref id="studentsApp"/></Arg>
    <Arg>jdbc/DS</Arg>
    <Arg>
        <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
            <Set name="Url">jdbc:mysql://localhost:3306/students</Set>
            <Set name="User">root</Set>
            <Set name="Password">root</Set>
        </New>
    </Arg>
</New>

并且发生了同样的错误。我尝试切换 jndi 范围 - 没有成功。
当我尝试查看 java:comp/ 以查看可用的 jndi 条目时,它什么也没有返回。使用的 jndi 列表代码:

Context t = (Context)new InitialContext().lookup("java:comp");
listContext(t, "");

private static final void listContext(Context ctx, String indent) {
    try {
        NamingEnumeration list = ctx.listBindings("");
        while (list.hasMore()) {
            Binding item = (Binding) list.next();
            String className = item.getClassName();
            String name = item.getName();
            System.out.println(indent + className + " " + name);
            Object o = item.getObject();
            if (o instanceof javax.naming.Context) {
                listContext((Context) o, indent + " ");
            }
        }
    } catch (NamingException ex) {
        System.out.println(ex);
    }
}

我确信 jetty-web.xml 已加载 - 我添加了:

<Call class="org.eclipse.jetty.util.log.Log" name="info"><Arg>Starting my super test application</Arg></Call>

jetty-web.xml 之上,它输出在服务器启动时。

然后,我决定从不可移植的 jetty-web.xml 迁移到 context.xml 文件,并将其放入 $JETTY_HOME/contexts 中,将 MySQL 连接器 jar 放入 $JETTY_HOME/lib/ext 中,它开始工作。

如果 JNDI 条目是从 jetty-web.xml 加载的,Jetty 似乎会出现问题。

Faced with the same problem.
My project/WEB-INF/jetty-env.xml was:

<New id="DS" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref id="studentsApp"/></Arg>
    <Arg>jdbc/DS</Arg>
    <Arg>
        <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
            <Set name="Url">jdbc:mysql://localhost:3306/students</Set>
            <Set name="User">root</Set>
            <Set name="Password">root</Set>
        </New>
    </Arg>
</New>

And same error was occured. I tried to switch jndi scopes - didn't work.
Event when I've tried to look into java:comp/ to look what jndi entries available, it returns nothing. Code for jndi listing used:

Context t = (Context)new InitialContext().lookup("java:comp");
listContext(t, "");

private static final void listContext(Context ctx, String indent) {
    try {
        NamingEnumeration list = ctx.listBindings("");
        while (list.hasMore()) {
            Binding item = (Binding) list.next();
            String className = item.getClassName();
            String name = item.getName();
            System.out.println(indent + className + " " + name);
            Object o = item.getObject();
            if (o instanceof javax.naming.Context) {
                listContext((Context) o, indent + " ");
            }
        }
    } catch (NamingException ex) {
        System.out.println(ex);
    }
}

I knew for sure that jetty-web.xml was loaded - I've added:

<Call class="org.eclipse.jetty.util.log.Log" name="info"><Arg>Starting my super test application</Arg></Call>

on top of the jetty-web.xml and it outputs at the server start.

Than, I decided to move from non-portable jetty-web.xml to context.xml file, placed it into $JETTY_HOME/contexts, placed MySQL connector jar into $JETTY_HOME/lib/ext and it started to work.

Looks like Jetty have a problem with JNDI entries if they are loaded from jetty-web.xml.

苏辞 2024-08-25 21:36:39

Jetty 文档指出了使用 JNDI 资源(例如数据库)的三步过程 。这些说明应适用于 Jetty 7 和 Jetty 8。

  1. 按如下方式编辑 $JETTY_HOME/start.ini 文件:
  2. 修改服务器 OPTIONS 以包含“plus”,例如:

    OPTIONS=Server,jsp,jmx,resources,websocket,ext,plus

  3. 在文件底部附近的配置文件部分中,在 etc/jetty.conf 后面添加以下行: xml:

    etc/jetty-plus.xml

Jetty documentation indicates a three-step process for using a JNDI resource such as a database. These instructions should apply to Jetty 7 and Jetty 8.

  1. Edit the $JETTY_HOME/start.ini file as follows:
  2. Modify the Server OPTIONS to include "plus", for example:

    OPTIONS=Server,jsp,jmx,resources,websocket,ext,plus

  3. Near the bottom of the file, in the section on Configuration files, add the following line after etc/jetty.xml:

    etc/jetty-plus.xml

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文