oracle javax.naming.NamingException:无法创建资源实例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10534826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javax.naming.NamingException: Cannot create resource instance
提问by jeff
I have tried a ton of variants of the below to get datasources to work but to no avail. I have been researching/trying for a few days now so I'm throwing in the towel and asking for help. First off though, I am having a hard time formating my code in this post. Nothing is getting indented and certain xml tags are disappearing. Probably stupid IE that work forces us to use....
我已经尝试了大量以下变体来使数据源正常工作,但无济于事。我已经研究/尝试了几天,所以我认输并寻求帮助。不过,首先,我很难在这篇文章中格式化我的代码。没有任何缩进,某些 xml 标签正在消失。可能是愚蠢的 IE 工作迫使我们使用....
web.xml
网页.xml
<resource-ref>
<res-ref-name>jdbc/nalabor</res-ref-name>
<res-type>oracle.jdbc.pool.OracleDataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<resource-ref>
<res-ref-name>jdbc/navarch</res-ref-name>
<res-type>oracle.jdbc.pool.OracleDataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
context.xml
上下文.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/nalabor" type="oracle.jdbc.pool.OracleDataSource"
maxActive="1" maxIdle="1" maxWait="10000"
factory="oracle.jdbc.pool.OracleDataSourceFactory"
url="jdbc:oracle:thin:@####.com:1521:SID"
driverClassName="oracle.jdbc.driver.OracleDriver"
username="###" password="###"/>
<Resource
name="jdbc/navarch" type="oracle.jdbc.pool.OracleDataSource"
maxActive="1" maxIdle="1" maxWait="10000"
factory="oracle.jdbc.pool.OracleDataSourceFactory"
url="jdbc:oracle:thin:@####.com:1521:SID"
driverClassName="oracle.jdbc.driver.OracleDriver"
username="###" password="###"/>
</Context>
Dao
道
try {
Context initContext = new InitialContext();
NamingEnumeration list = initContext.list("java:/comp/env");
System.out.println("Listing NamingEnumeration For java:/comp/env");
while (list.hasMore()) {
NameClassPair nc = (NameClassPair)list.next();
System.out.println("Name Class Pair = " + nc);
}
list = initContext.list("java:/comp/env/jdbc");
System.out.println("Listing NamingEnumeration java:/comp/env/jdbc");
while (list .hasMore()) {
NameClassPair nc = (NameClassPair)list .next();
System.out.println("Name Class Pair = " + nc);
}
Context envContext = (Context) initContext.lookup("java:/comp/env");
ods = (OracleDataSource) envContext.lookup("jdbc/nalabor");
} catch (Exception ex) {
System.out.println("ERORRRRRRRR AGAIN!");
ex.printStackTrace();
}
Stack
堆
Listing NamingEnumeration For java:/comp/env
Name Class Pair = mailClient: java.lang.String
Name Class Pair = siteConnCache: java.lang.String
Name Class Pair = jdbc: org.apache.naming.NamingContext
Name Class Pair = sitePOCEmail: java.lang.String
Name Class Pair = siteFilePrefix: java.lang.String
Name Class Pair = siteName: java.lang.String
Name Class Pair = siteEmail: java.lang.String
Listing NamingEnumeration java:/comp/env/jdbc
Name Class Pair = nalabor: org.apache.naming.ResourceRef
Name Class Pair = navarch: org.apache.naming.ResourceRef
ERORRRRRRRR AGAIN!
javax.naming.NamingException: Cannot create resource instance
at org.apache.naming.factory.ResourceFactory.getObjectInstance(ResourceFactory.java:167)
at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:314)
at org.apache.naming.NamingContext.lookup(NamingContext.java:834)
at org.apache.naming.NamingContext.lookup(NamingContext.java:181)
at org.apache.naming.NamingContext.lookup(NamingContext.java:822)
at org.apache.naming.NamingContext.lookup(NamingContext.java:194)
at com.gdebI.rozycki.bsc.data.LaborDAO.getWeightedLabor(LaborDAO.java:91)
at com.gdebI.rozycki.bsc.controller.action.WeightedLabor.getList(WeightedLabor.java:66)
at com.gdebI.rozycki.controller.action.ListAction.service(ListAction.java:38)
WEB-INF/lib
网络信息/库
ojdbc14.jar
回答by Paul
I'm not sure why you listed your resources in your web.xml but I think you are including an extra / that is causing the problem. I've encountered this exception when the name can't be found. Try this (Java 6+):
我不确定您为什么在 web.xml 中列出了您的资源,但我认为您包含了导致问题的额外 /。当找不到名称时,我遇到了这个异常。试试这个(Java 6+):
OracleDataSource ods = InitialContext.doLookup("java:comp/env/jdbc/nalabor");
or this for Java 5 and below:
或者对于 Java 5 及以下版本:
InitialContext ic = new InitialContext();
OracleDataSource ods = (OracleDataSource)ic.lookup("java:comp/env/jdbc/nalabor");
回答by Arthur Dong
I meet this problem and I solve it . notice your the order of elements in your web.xml. I resort the order of element in My web app's web.xml as the http://wiki.apache.org/tomcat/Specificationssays and it works.
我遇到了这个问题,我解决了它。注意 web.xml 中元素的顺序。我按照http://wiki.apache.org/tomcat/Specifications所说的那样在我的 web 应用程序的 web.xml 中使用元素的顺序并且它可以工作。