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

时间:2010-01-25 11:10:15

标签: java mysql jdbc jetty connection-pooling

我的配置文件

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

无论哪种方式开始码头,我都遇到了以下错误:


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文件:
    • 公地DBCP-1.2.2.jar
    • MySQL的连接器的Java-5.1.10-bin.jar

谢谢!

4 个答案:

答案 0 :(得分:10)

注意:以下假设您使用的是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。

答案 1 :(得分:4)

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

答案 2 :(得分:0)

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

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

答案 3 :(得分:-1)

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

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

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

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

    etc/jetty-plus.xml