4

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>

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)
Joakim Erdfelt
  • 46,896
  • 7
  • 86
  • 136
FiendFyre
  • 157
  • 3
  • 17
  • Could it be because you have id set to `DStest` in your `` statement? – HeatfanJohn Jul 29 '13 at 20:16
  • Looking at [configuring datasources in Jetty](http://www.eclipse.org/jetty/documentation/current/jndi-configuration.html#configuring-datasources) more I can see that my previous comment is not correct. – HeatfanJohn Jul 29 '13 at 20:30
  • I didn't get you! why will id cause an issue, when I'm looking up JNDI Resouce using name? – FiendFyre Jul 29 '13 at 20:31

2 Answers2

3

Have you tried to change the string "java:comp/env/jdbc/testDS" with just "jdbc/testDS"? I think that is a jetty 9 'feature'.

john.hestad
  • 81
  • 1
  • 3
0

i had the same problem. i used <Arg>java:comp/env/jdbc/testdb</Arg> instead of <Arg>jdbc/testdb</Arg> and it worked

<New id="JndiResource" class="org.eclipse.jetty.plus.jndi.Resource">
 <Arg></Arg>
 <Arg>java:comp/env/jdbc/testdb</Arg>
 <Arg>
  <New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
   <Set name="Url">jdbc:mysql://127.0.0.1:3306/mydb</Set>
   <Set name="User">user</Set>
   <Set name="Password"></Set>
  </New>
 </Arg>
</New>

and of course in my lookup i used java:comp/env/jdbc/bratedb

Sameeh Harfoush
  • 610
  • 1
  • 8
  • 22