代码之家  ›  专栏  ›  技术社区  ›  Alex Luya

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

  •  5
  • Alex Luya  · 技术社区  · 15 年前

    我的配置文件

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

    项目/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文件

    谢谢您!

    4 回复  |  直到 8 年前
        1
  •  10
  •   ROMANIA_engineer Alexey    8 年前

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

    问题是你缺少了一个额外的间接层。即使你已经在你的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>
    

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

        2
  •  4
  •   Sean    14 年前

    这让我整天发疯。仅供参考,我在6.1.22号码头工作。第一篇文章的注释是正确的——我在WEB-INF中有一个文件名jetty-env.xml,它被忽略了。我将其重命名为jetty-web.xml 为新指令的第二个参数指定数据源的完整名称(即Java:COMP/Env/JDBC/MyDATA)。

        3
  •  0
  •   CSchulz cL83    13 年前

    面临同样的问题。 我的 项目/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网站 上下文.xml 文件,放入 $jetty_主页/上下文 ,将mysql connector jar放入 $jetty_主页/lib/ext 开始工作了。

    如果jndi条目是从 jetty-web.xml网站

        4
  •  -1
  •   Mike Christianson    13 年前

    Jetty documentation indicates a three-step process 用于使用jndi资源,如数据库。这些说明应适用于7号码头和8号码头。

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

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

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

      etc/jetty-plus.xml