Jetty mysql connection-pool configuration error: javax.naming.NameNotFoundException; remaining name 'env/jdbc/---(mysql 5.0+jetty 7.0.1)

22,635

Solution 1

Note: The following assumes you're using Jetty 7.2.2+ (probably works with any Jetty 7.0+).

The problem is you're missing ONE extra layer of indirection. Even though you've configured Jetty in your webapp, you still need to tell the Jetty container that it needs to look for jetty-env.xml in your webapp. To do that, add the following to your jetty.xml (under ${JETTY_INSTALL_DIR}/etc:

<Call name="setAttribute">
  <Arg>org.eclipse.jetty.webapp.configuration</Arg>
  <Arg>
      <Array type="java.lang.String">
          <Item>org.eclipse.jetty.webapp.WebInfConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.WebXmlConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.MetaInfConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.FragmentConfiguration</Item>
          <Item>org.eclipse.jetty.plus.webapp.EnvConfiguration</Item>
          <Item>org.eclipse.jetty.plus.webapp.PlusConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.JettyWebXmlConfiguration</Item>
          <Item>org.eclipse.jetty.webapp.TagLibConfiguration</Item>
      </Array>
  </Arg>
</Call>

NOW Jetty will know to parse the jetty-env.xml you've created.

Solution 2

This drove me nuts all day long. FYI, I am working with Jetty 6.1.22. The comment to the first post is on track -- I had a file name jetty-env.xml in WEB-INF, and it was being ignored. I renamed it to jetty-web.xml and specified the full name of the data source (i.e. java:comp/env/jdbc/mydatasource) for the second argument of the New directive and it worked.

Share:
22,635
Alex Luya
Author by

Alex Luya

Updated on September 13, 2020

Comments

  • Alex Luya
    Alex Luya over 3 years

    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!