2011-07-20 66 views
3

我想使用连接池与码头6,但到目前为止,它给我很多麻烦。Jetty 6:无法创建上下文(NoInitialContextException)

InitialContext context = new InitialContext(); 

它不会抛出异常,但是当我检查上下文变量我看到这一点,这是有点可疑:

enter image description here

通知的defaultInitCtx中=空值。

后,我试图得到一个数据源与行:

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

,现在我得到一个异常说,没有初始上下文:

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 

很显然,我必须做的事情错,但我无法找到什么... 如果有人能帮助,我将不胜感激!

这里是我的的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> 

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> 

WEB-INF /码头-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> 
+0

看起来这个链接可能会证明是有用的。 http://stackoverflow.com/questions/1525385/noinitialcontextexception-errori/1989115#1989115 – 2011-07-22 03:25:59

回答

0

您必须创建带有工厂值的jndi.properties文件以用于初始化并将该文件放在类路径中。

如果您使用的码头7,属性将

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

PS:我也有这个问题遇到固定这种方式。我使用码头7来达到我的目的。如果您使用的是旧版本,只需确定相同的值(如果它们不同)。

对于码头6,相信性能应该是(看API(我没有测试过这一点)

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

在客户端代码之后,您可以检查工厂是否正确初始化

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); 
} 
+1

感谢您的答案。我的项目的这一部分现在暂时搁置,但我会在恢复开发时尝试尝试。如果有一些新的发展,我会保持这篇文章更新! – Mathieu

相关问题