gpt4 book ai didi

java - JNDI 查找失败(NameNotFoundException)

转载 作者:塔克拉玛干 更新时间:2023-11-03 03:25:17 26 4
gpt4 key购买 nike

更新:好吧,似乎只有当我尝试在本地调试时才会出现这个问题。在 Jetty(不是同一台机器)上发布时效果很好。本地调试时是否可以配置Run-Jetty-Run使用的jetty.xml文件?

我只是浪费了一个下午试图找到这个问题的原因,但没有成功......

我正在为我的应用程序设置一个带有 c3p0 的连接池。顺便说一句,我正在使用 Jetty 7。问题是当我执行 context.lookup 获取数据源时,出现此异常:

javax.naming.NameNotFoundException; remaining name 'env/jdbc/DSTest'
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:634)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:665)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:680)
at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:113)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at com.see.metrics.SqlHelper.Initialize(SqlHelper.java:68)
at com.see.metrics.Metrics.Initialize(Metrics.java:45)
at com.see.game.GameProducer.generateMetadata(GameProducer.java:223)
at org.odata4j.producer.mongodb.MongoProducer.initMongo(MongoProducer.java:98)
at org.odata4j.producer.mongodb.MongoProducerFactory.create(MongoProducerFactory.java:55)
at org.odata4j.producer.resources.ODataProducerProvider.newProducerFromFactory(ODataProducerProvider.java:66)
at org.odata4j.producer.resources.ODataProducerProvider.getInstance(ODataProducerProvider.java:48)
at org.odata4j.producer.resources.ODataProducerProvider.getInstance(ODataProducerProvider.java:1)
at com.sun.jersey.core.impl.provider.xml.LazySingletonContextProvider.get(LazySingletonContextProvider.java:80)
at com.sun.jersey.core.impl.provider.xml.LazySingletonContextProvider.access$000(LazySingletonContextProvider.java:52)
at com.sun.jersey.core.impl.provider.xml.LazySingletonContextProvider$1.getValue(LazySingletonContextProvider.java:69)
at com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable$1.getValue(AbstractHttpContextInjectable.java:100)
at com.sun.jersey.server.impl.inject.InjectableValuesProvider.getInjectableValues(InjectableValuesProvider.java:43)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$EntityParamInInvoker.getParams(AbstractResourceMethodDispatchProvider.java:119)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:166)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:259)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:990)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:538)
at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:478)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119)
at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:517)
at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:225)
at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:937)
at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:406)
at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:183)
at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:871)
at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:110)
at org.eclipse.jetty.server.Server.handle(Server.java:346)
at org.eclipse.jetty.server.HttpConnection.handleRequest(HttpConnection.java:589)
at org.eclipse.jetty.server.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:1048)
at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:601)
at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:214)
at org.eclipse.jetty.server.HttpConnection.handle(HttpConnection.java:411)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:535)
at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:40)
at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:529)
at java.lang.Thread.run(Thread.java:662)

我有一个 jndi.properties 文件来配置我的 InitialContext(位于我的资源文件夹 src/main/resources 中)

java.naming.factory.url.pkgs=org.eclipse.jetty.jndi
java.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory

我在 webapp/WEB-INF 文件夹中还有一个 jetty-env.xml 文件:

<Configure class="org.mortbay.jetty.webapp.WebAppContext">

<!--<Set name="ConfigurationClasses">
<Ref id="plusConfig"/>
</Set> -->

<!-- MySql datasource org.eclipse.jetty.plus.jndi.Resource Factory-->
<New id="DSTest" class="org.mortbay.jetty.plus.naming.Resource">
<Arg></Arg>
<Arg>jdbc/DSTest</Arg>
<Arg>
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
<Set name="driverClassName">com.mysql.jdbc.Driver</Set>
<Set name="url">jdbc:mysql://host/</Set>
<Set name="username">username</Set>
<Set name="password">password</Set>
<Set name="checkoutTimeout">5000</Set>
<Set name="initialPoolSize">10</Set>
<Set name="maxIdleTime">30</Set>
<Set name="maxPoolSize">160</Set>
<Set name="minPoolSize">10</Set>
<Set name="maxStatements">200</Set>
<Set name="maxConnectionAge">0</Set>
<Set name="acquireIncrement">15</Set>
</New>
</Arg>
</New>
</Configure>

然后我在我的 web.xml 文件中添加了这些行:

<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/DSTest</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>

我似乎已经遵循了所有必需的步骤,但在进行查找时我仍然得到 javax.naming.NameNotFoundException...

有什么提示吗?

谢谢!

编辑:我忘了说明我是如何查找的。

InitialContext ctx = new InitialContext();
DataSource dataSource = (DataSource)ctx.lookup("java:comp/env/jdbc/DSTest");

最佳答案

愚蠢的问题但是这个页面http://docs.codehaus.org/display/JETTY/JNDI#JNDI-resref声明将 JNDI 与 Jetty 一起使用是一项需要启用的可选功能。

具体来说,执行此操作的类是 org.mortbay.jetty.plus.webapp.Configuration,当我们定义 org.mortbay 时,我们在要应用于 webapp 的配置列表中指定它的名称.jetty.webapp.WebAppContext 用于它。

关于java - JNDI 查找失败(NameNotFoundException),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7972179/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com