Jetty mysql 连接池配置错误:javax.naming.NameNotFoundException;剩余名称 'env/jdbc/---(mysql 5.0+jetty 7.0.1)
我的配置文件
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
注意:以下假设您使用的是 Jetty 7.2.2+(可能适用于任何 Jetty 7.0+)。
问题是您缺少一层额外的间接层。即使您已经在 Web 应用程序中配置了 Jetty,您仍然需要告诉 Jetty 容器它需要在您的 Web 应用程序中查找 jetty-env.xml。为此,请将以下内容添加到您的 jetty.xml 中(在 ${JETTY_INSTALL_DIR}/etc 下):
现在 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:
NOW Jetty will know to parse the jetty-env.xml you've created.
这让我一整天都发疯。仅供参考,我正在使用 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.
面临同样的问题。
我的 project/WEB-INF/jetty-env.xml 是:
并且发生了同样的错误。我尝试切换 jndi 范围 - 没有成功。
当我尝试查看 java:comp/ 以查看可用的 jndi 条目时,它什么也没有返回。使用的 jndi 列表代码:
我确信 jetty-web.xml 已加载 - 我添加了:
在 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:
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:
I knew for sure that jetty-web.xml was loaded - I've added:
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.
Jetty 文档指出了使用 JNDI 资源(例如数据库)的三步过程 。这些说明应适用于 Jetty 7 和 Jetty 8。
$JETTY_HOME/start.ini
文件:修改服务器
OPTIONS
以包含“plus”,例如:OPTIONS=Server,jsp,jmx,resources,websocket,ext,plus
在文件底部附近的配置文件部分中,在
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.
$JETTY_HOME/start.ini
file as follows:Modify the Server
OPTIONS
to include "plus", for example:OPTIONS=Server,jsp,jmx,resources,websocket,ext,plus
Near the bottom of the file, in the section on Configuration files, add the following line after
etc/jetty.xml
:etc/jetty-plus.xml