views:

818

answers:

1

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!

A: 

Faced with the same problem. My project/WEB-INF/jetty-env.xml was:

<New id="DS" class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg><Ref id="studentsApp"/></Arg>
    <Arg>jdbc/DS</Arg>
    <Arg>
        <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
            <Set name="Url">jdbc:mysql://localhost:3306/students</Set>
            <Set name="User">root</Set>
            <Set name="Password">root</Set>
        </New>
    </Arg>
</New>

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:

Context t = (Context)new InitialContext().lookup("java:comp");
listContext(t, "");

private static final void listContext(Context ctx, String indent) {
    try {
        NamingEnumeration list = ctx.listBindings("");
        while (list.hasMore()) {
            Binding item = (Binding) list.next();
            String className = item.getClassName();
            String name = item.getName();
            System.out.println(indent + className + " " + name);
            Object o = item.getObject();
            if (o instanceof javax.naming.Context) {
                listContext((Context) o, indent + " ");
            }
        }
    } catch (NamingException ex) {
        System.out.println(ex);
    }
}

I knew for sure that jetty-web.xml was loaded - I've added:

<Call class="org.eclipse.jetty.util.log.Log" name="info"><Arg>Starting my super test application</Arg></Call>

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.

Alexander Ulizko