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

Ale*_*uya 5 java mysql connection-pooling jetty jdbc

我的配置文件

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>
Run Code Online (Sandbox Code Playgroud)

项目/ 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>
Run Code Online (Sandbox Code Playgroud)

要调用的代码:

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

脚本启动jetty:

java -DOPTIONS=plus -jar start.jar

java -jar start.jar
Run Code Online (Sandbox Code Playgroud)

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


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)
Run Code Online (Sandbox Code Playgroud)

问题是:

  • 这里有什么问题?
  • 还需要其他配置吗?
  • 放置以下jar文件的位置:
    • 公地DBCP-1.2.2.jar
    • MySQL的连接器的Java-5.1.10-bin.jar

谢谢!

小智 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>
Run Code Online (Sandbox Code Playgroud)

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


小智 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>
Run Code Online (Sandbox Code Playgroud)

并发生了同样的错误。我尝试切换 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

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

<Call class="org.eclipse.jetty.util.log.Log" name="info"><Arg>Starting my super test application</Arg></Call>
Run Code Online (Sandbox Code Playgroud)

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

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

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