Question

More specifically i got executable war containing all the dependencies. Application starts without jndi datasource like a charm runing it with jndi datasource failed though. I believe there is no place for jetty.xml in such a configuration so jetty-env.xml is no go either since jetty wont read it by default. I attempted to use jetty-web.xml for jndi datasource configuration but jetty fails to deploy application returning 503 error code. I am using Jetty9-M4. Attempted tomcat pooling and BoneCP both with same result.

Starter class:

import java.net.URL;
import java.security.ProtectionDomain;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.webapp.WebAppContext;


public final class Loader {

    public static void main(String[] args) throws Exception
    {
         String jetty_home = "..";
         int appli_port = 8080;
         Server server = new Server(appli_port);
         ProtectionDomain protectionDomain = Loader.class.getProtectionDomain();
         URL location = protectionDomain.getCodeSource().getLocation();
         WebAppContext webapp = new WebAppContext();
         webapp.setContextPath("/");
         webapp.setWar(location.toExternalForm());    
         server.setHandler(webapp);

         server.start();
         server.join();
    }
}

jetty-web.xml:

<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <New id="testDS" class="org.eclipse.jetty.plus.jndi.Resource">
        <Arg></Arg>
        <Arg>jdbc/testDS</Arg>
        <Arg>
            <New class="com.jolbox.bonecp.BoneCPDataSource">
                <Set name="driverClass">org.postgresql.Driver</Set>
                <Set name="jdbcUrl">jdbc:postgresql://localhost:5432/testDS</Set>
                <Set name="username">name</Set>
                <Set name="password">password</Set>
                <Set name="minConnectionsPerPartition">5</Set>
                <Set name="maxConnectionsPerPartition">50</Set>
                <Set name="acquireIncrement">5</Set>
                <Set name="idleConnectionTestPeriod">30</Set>
            </New>
        </Arg>
    </New>
</Configure>

I suspect that its either to late for jndi on jetty-web phase or config is just wrong.

Sorry for bad english.


Forgot to mention i am using Hibernate as ORM.

web.xml contains:

<resource-ref>
    <description>Postgres datasource</description>
    <res-ref-name>jdbc/testDS</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

Hibernate.cfg.xml contains

<property name="hibernate.connection.datasource">java:comp/env/jdbc/testDS</property>

attempted

<property name="hibernate.connection.datasource">jdbc/testDS</property>

not working either.


As a little side note jetty-web.xml placed in WEB-INF of your war is fine for JNDI resource configuration and it is used by default.

Was it helpful?

Solution

jetty-env.xml files should be picked up from WEB-INF/jetty-env.xml automatically, see here: http://www.eclipse.org/jetty/documentation/current/jetty-env-xml.html. I would suggest going with jetty-env.xml as opposed to jetty-web.xml.

Can you also show how you are performing the JNDI look up from within your application? Maybe the JNDI datasource is being initialized correctly but the JNDI name isn't correct?

There are some jetty JNDI examples here: http://www.eclipse.org/jetty/documentation/current/jndi-datasource-examples.html

Update: Seems like Jetty won't pick it up automatically when running embedded. Try this:

public static void main(String[] args) throws Exception
{
    String jetty_home = "..";
    int appli_port = 8080;
    Server server = new Server(appli_port);
    ProtectionDomain protectionDomain = Loader.class.getProtectionDomain();
    URL location = protectionDomain.getCodeSource().getLocation();
    WebAppContext webapp = new WebAppContext();
    webapp.setContextPath("/");
    webapp.setWar(location.toExternalForm());

    // setup JNDI
    EnvConfiguration envConfiguration = new EnvConfiguration();
    URL url = new File("path to jetty-env.xml").toURI().toURL();
    envConfiguration.setJettyEnvXml(url);
    webapp.setConfigurations(new Configuration[] {new WebInfConfiguration(), envConfiguration, new WebXmlConfiguration()});

    server.setHandler(webapp); 
    server.start();
    server.join();
}

(See http://blog.armhold.com/2011/12/27/how-to-get-jndi-working-with-wicket-1-5-and-jetty-7-5/)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top