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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2131798/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:54:38  来源:igfitidea点击:

Jetty mysql connection-pool configuration error: javax.naming.NameNotFoundException; remaining name 'env/jdbc/---(mysql 5.0+jetty 7.0.1)

javamysqljdbcjettyconnection-pooling

提问by Alex Luya

My configuration files

我的配置文件

project/WEB-INF/web.xml:

项目/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:

项目/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
  • 这里有什么问题?
  • 还需要什么其他配置吗?
  • 放置以下 jar 文件的位置:
    • commons-dbcp-1.2.2.jar
    • mysql-connector-java-5.1.10-bin.jar

Thank you!

谢谢!

回答by Alexander Ulizko

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

面临同样的问题。我的项目/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>

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:

并且发生了同样的错误。我试图切换 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);
    }
}

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

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

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

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

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

Than, I decided to move from non-portable jetty-web.xmlto context.xmlfile, placed it into $JETTY_HOME/contexts, placed MySQL connector jar into $JETTY_HOME/lib/extand it started to work.

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

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

如果 JNDI 条目是从jetty-web.xml加载的,那么看起来 Jetty 有问题。

回答by DeAngelo Lampkin

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

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

The problem is you're missing ONE extra layer of indirection. Even though you've configured Jetty in your webapp, you stillneed 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:

问题是您缺少一个额外的间接层。即使你已经在你的 webapp 中配置了 Jetty,你仍然需要告诉 Jetty 容器它需要在你的 webapp 中查找 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>

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

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

回答by Sean

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

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

回答by Mike Christianson

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

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

  1. Edit the $JETTY_HOME/start.inifile as follows:
  2. Modify the Server OPTIONSto 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

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

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

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

    etc/jetty-plus.xml