Java Jetty 6:无法创建上下文(NoInitialContextException)
我想使用Jetty 6的连接池,但到目前为止,它给我带来了很多麻烦 我知道 它不会引发异常,但当我检查上下文变量时,我看到了以下内容,这有点可疑: 请注意defaultInitCtx=null 我尝试获取数据源后的行:Java Jetty 6:无法创建上下文(NoInitialContextException),java,eclipse,jdbc,jetty,jndi,Java,Eclipse,Jdbc,Jetty,Jndi,我想使用Jetty 6的连接池,但到目前为止,它给我带来了很多麻烦 我知道 它不会引发异常,但当我检查上下文变量时,我看到了以下内容,这有点可疑: 请注意defaultInitCtx=null 我尝试获取数据源后的行: Datasource dataSource = (DataSource)context.lookup("java:comp/env/MySQLDB"); 现在我得到一个例外,说没有初始上下文: javax.naming.NoInitialContextException: N
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>
<?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>
<?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>
... (除我添加的行以外的所有默认行)
org.eclipse.jetty.webapp.configuration
org.eclipse.jetty.webapp.webinf配置
org.eclipse.jetty.webapp.WebXmlConfiguration
org.eclipse.jetty.webapp.MetaInfConfiguration
org.eclipse.jetty.webapp.FragmentConfiguration
org.eclipse.jetty.plus.webapp.EnvConfiguration
org.eclipse.jetty.plus.webapp.plus配置
org.eclipse.jetty.webapp.JettyWebXmlConfiguration
org.eclipse.jetty.webapp.TagLibConfiguration
MyWEB-INF/WEB.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>
<?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>
<?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>
试验
违约
违约
违约
违约
/违约
数据库连接
MySQLDB
javax.sql.DataSource
容器
MyWEB-INF/jetty env.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>
<?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>
<?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>
MySQLDB
com.mysql.jdbc.Driver
jdbc:mysql://host_ip
用户名
密码
容器
-1
30
10000
600000
MySQLDB
真的
5000
10000
javax.sql.DataSource
您必须创建jndi.properties文件,其中包含用于初始化的工厂值,并将该文件放入类路径中
如果您使用的是jetty 7,则属性将为
java.naming.factory.url.pkgs=org.eclipse.jetty.jndi
java.naming.factory.initial=org.eclipse.jetty.jndi.InitialContextFactory
PS:我也遇到了这个问题,并用这种方式解决了它。我用的是7号码头。如果您使用的是旧版本,只需标识相同的值(如果它们不同)
对于jetty 6,我认为属性应该是(请看)(我还没有测试过)
稍后在客户端代码中,您可以检查工厂是否正确初始化
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);
}
看起来这个链接可能很有用。谢谢你的回答。我的项目的这一部分现在有点搁置,但我一定会在恢复开发时尝试这一点。如果有新的进展,我会更新这篇文章!