NameNotFoundException Jetty 9 JNDI查找 [英] NameNotFoundException Jetty 9 JNDI lookup
问题描述
我需要使用C3p0连接池为H2数据库实现Jetty 9.0.3 Web服务器的JNDI,将H2和C3p0 jars都放置在JETTY-HOME目录的lib/ext中,并创建了一个jetty-env我的WEB-INF中的.xml文件.
I need to implement JNDI for Jetty 9.0.3 web server for H2 database using C3p0 connection pooling, I've placed both H2 and C3p0 jars in lib/ext of JETTY-HOME directory and I've created a jetty-env.xml file in my WEB-INF.
WEB-INF/jetty-env.xml
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<New id="DSTest" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg>jdbc/testDS</Arg>
<Arg>
<New class="com.mchange.v2.c3p0.ComboPooledDataSource">
<Set name="driverClass">org.h2.Driver</Set>
<Set name="jdbcUrl">jdbc:h2:/C:/data/test</Set>
<Set name="user">sa</Set>
<Set name="password"></Set>
</New>
</Arg>
</New>
</Configure>
我正在通过以下类的main方法创建Jetty服务器实例来实现启用了plus配置的嵌入式码头:
I'm implementing embedded jetty with plus configuration enabled, by creating an instance of Jetty server from the main method of below class :
WebServer.java
import java.io.File;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.util.resource.ResourceCollection;
import org.eclipse.jetty.webapp.WebAppContext;
public class WebServer
{
public static void main(String[] args)
{
// Creating Jetty Server on port 8080
Server webServer = new Server(8080);
org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(webServer);
classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration",
"org.eclipse.jetty.plus.webapp.PlusConfiguration");
WebAppContext wac = new WebAppContext();
// Set WAR Path to WebAppcontext from disk
File warPath = new File("C:/Users/XXXX/src/com/UI");
wac.setWar(warPath.getAbsolutePath());
wac.setContextPath("/");
wac.setBaseResource(new ResourceCollection(new String[] { "./WebContent", "build/classes" }));
webServer.setHandler(wac);
try
{
InitialContext ic = new InitialContext();
DataSource myDS = (DataSource)ic.lookup("java:comp/env/jdbc/testDS");
System.out.println("param ::: "+myDS);
webServer.start();
webServer.join();
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
我收到以下错误,该如何解决?
I get following error, how can I resolve this?
javax.naming.NameNotFoundException; remaining name 'env/jdbc/testDS'
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:505)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:536)
at org.eclipse.jetty.jndi.NamingContext.lookup(NamingContext.java:551)
at org.eclipse.jetty.jndi.java.javaRootURLContext.lookup(javaRootURLContext.java:117)
at javax.naming.InitialContext.lookup(InitialContext.java:411)
at com.server.WebServer.main(WebServer.java:37)
推荐答案
您是否尝试仅使用"jdbc/testDS"来更改字符串"java:comp/env/jdbc/testDS"?我认为这是码头9的特色".
Have you tried to change the string "java:comp/env/jdbc/testDS" with just "jdbc/testDS"? I think that is a jetty 9 'feature'.
这篇关于NameNotFoundException Jetty 9 JNDI查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!