开发者

Jetty 6: Cannot create context (NoInitialContextException)

I want to use connection pooling with Jetty 6, but so far it is giving my a lot of troubles.

I do

InitialContext context = new InitialContext();

It doesn't throw an exception, but when I inspect the context variable I see this, which is kinda suspect:

Jetty 6: Cannot create context (NoInitialContextException)

开发者_如何学C

Notice the defaultInitCtx=null.

The line after I try to get a datasource with:

Datasource dataSource = (DataSource)context.lookup("java:comp/env/MySQLDB");

and now I get an exception saying that there is no initial context:

javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial

Obviously, I must be doing something wrong, but I just can't find what... If anyone could help, I would appreciate it!

Here is my jetty.xml:

<Configure id="Server" class="org.mortbay.jetty.Server">

... (all default except those lines I added)

  <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>
</Configure>

My WEB-INF/web.xml:

<?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" id="WebApp_ID" version="2.5">
  <display-name>Test</display-name>
  <servlet>
    <description></description>
    <display-name>Default</display-name>
    <servlet-name>Default</servlet-name>
    <servlet-class>Default</servlet-class>
 </servlet>
  <servlet-mapping>
    <servlet-name>Default</servlet-name>
    <url-pattern>/Default</url-pattern>
  </servlet-mapping>
  <resource-ref>
    <description>DB Connection</description>
    <res-ref-name>MySQLDB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
  </resource-ref>
</web-app>

My WEB-INF/jetty-env.xml:

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

<Configure class="org.mortbay.jetty.webapp.WebAppContext">  
<Set name="ConfigurationClasses">
   <Ref id="plusConfig"/>
</Set>


<!-- MySql datasource -->
<New id="MySQLDB" class="org.mortbay.jetty.plus.naming.Resource">
       <Arg></Arg>
       <Arg>MySQLDB</Arg>
       <Arg>
         <New class="org.apache.commons.dbcp.BasicDataSourceFactory">
           <Set name="driverClassName">com.mysql.jdbc.Driver</Set>
           <Set name="url">jdbc:mysql://host_ip</Set>
           <Set name="username">username</Set>
           <Set name="password">password</Set>
           <Set name="auth">Container</Set>
           <Set name="maxActive">-1</Set>
           <Set name="maxIdle">30</Set>
           <Set name="maxWait">10000</Set>
           <Set name="minEvictableIdleTimeMillis">600000</Set>
           <Set name="name">MySQLDB</Set>
           <Set name="removeAbandoned">true</Set>
           <Set name="removeAbandonedTimeout">5000</Set>
           <Set name="timeBetweenEvictionRunsMillis">10000</Set>
           <Set name="type">javax.sql.DataSource</Set>
        </New>
       </Arg>
     </New>


You have to create jndi.properties file with values for factory to be used for initializing and put the file in the class path.

If you are using jetty 7, the properties will be

java.naming.factory.url.pkgs=org.eclipse.jetty.jndi
java.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory

PS: I have also come across with the problem and fixed it this way. I was using jetty 7 for my purpose. If you are using older versions, just identify the values for the same(if they are different).

For jetty 6, I believe the properties should be (look at the API) (I haven't tested this)

java.naming.factory.url.pkgs=org.mortbay.naming
java.naming.factory.initial=org.mortbay.naming.InitialContextFactory

Later in your client code, you can check whether the factory is initialized properly

try {
    InitialContext ic = new InitialContext();
    return (DataSource) ic.lookup("java:comp/env/MySQLDB");
} catch (NamingException e) {
    logger.error("JNDI error while retrieving datasource" , e);
    throw new Exception(e);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜