开发者

Jetty 6 Eclipse Datasource NameNotFoundException

I try to configure a data source for a Jetty 6 Server in my Eclipse Project.

I include these jars in my lib folder:

common-dbcp-1.4.jar
commons-pool-1.5.6.jar
jetty-naming-6.1.14.jar
the jetty-plus-6.1.11.jar

Here is the jetty-env.xml from the WEB-INF:

<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<Configure id="myapp" class="org.mortbay.jetty.webapp.WebAppContext">
    <New id="DSTest" class="org.mortbay.jetty.plus.naming.EnvEntry">
        <Arg><Ref id="myapp" /></Arg>
        <Arg>jdbc/DSTest</Arg>
        <Arg>
            <New class="org.apache.commons.dbcp.BasicDataSource">
                <Set name="driverClassName">org.postgresql.Driver</Set>
                <Set name="url">dbc:postgresql://localhost:5432/schoeneberg</Set>
                <Set name="username">postgres</Set>
                <Set name="password">deluxe</Set>
            </New>
        </Arg>
    </New>
</Configure>

My web.xml from the WEB-INF

<?xml version="1.0" encoding="utf-8"?>
<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

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

    <servlet>
        <servlet-name>test</servlet-name>
        <servlet-class>de.test.Test</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>test</servlet-name>
        <url-pattern>/test</url-pattern>
    </servlet-mapping>
</web-app>

and here the Servlet Code:

@SuppressWarnings("serial")
public class Test extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        try {

            Context env = (Context) new InitialContext().lookup("java:comp/env/jdbc/DSTest");
        } catch(Exception ex) {
            ex.printStackTrace();
        }
    }
}

Seems to be a simple Example for Datasources and Connection Pooling in Jetty 6. But I always get this Exception:

javax.naming.NameNotFoundException; remaining name 'env/jdbc/DSTest'
    at org.mortbay.naming.NamingContext.lookup(NamingContext.java:634)
    at org.mortbay.naming.NamingContext.lookup(NamingContext.java:665)
    at org.mortbay.naming.NamingContext.lookup(NamingContext.java:680)
    at org.mortbay.naming.java.javaRootURLContext.lookup(javaRootURLContext.java:112)
    at javax.naming.InitialContext.lookup(InitialContext.java:392)
    at de.test.Test.doGet(Test.java:25)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:487)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:362)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:181)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:729)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:405)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.handler.RequestLogHandler.handle(RequestLogHandler.java:49)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:324)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:505)
    at org.mortb开发者_如何学JAVAay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:829)
    at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:513)
    at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:211)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:380)
    at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:395)
    at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:488)

Anybody an idea?

Thanks for help!


try only with the resource name:

final Context ctx = new InitialContext();
final DataSource ds = (DataSource) ctx.lookup("jdbc/DSTest");

It's works for me

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜