我需要使用 C3p0 连接池为 H2 数据库的 Jetty 9.0.3 Web 服务器实现 JNDI,我已将 H2 和 C3p0 jar 放在 JETTY-HOME 目录的 lib/ext 中,并创建了一个 jetty-env.xml 文件在我的 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>
我正在通过以下类的主要方法创建一个 Jetty 服务器实例来实现启用了 plus 配置的嵌入式码头:
网络服务器.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();
}
}
}
我收到以下错误,我该如何解决?
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)