javax.naming.NamingException:无法创建资源实例

时间:2012-05-10 13:16:51

标签: java oracle datasource

我已经尝试了下面的大量变体来使数据源工作但无济于事。我现在一直在研究/尝试几天,所以我正在撒谎并寻求帮助。首先,我很难在这篇文章中编写我的代码。什么都没有缩进,某些xml标签正在消失。可能是愚蠢的IE工作迫使我们使用....

的web.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 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>

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();
}

堆栈

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

2 个答案:

答案 0 :(得分:1)

我不确定你为什么要在web.xml中列出你的资源,但我认为你要包含一个额外/导致问题的资源。当找不到名字时,我遇到了这个例外。试试这个(Java 6 +):

OracleDataSource ods = InitialContext.doLookup("java:comp/env/jdbc/nalabor");

或Java 5及以下版本:

InitialContext ic = new InitialContext();
OracleDataSource ods = (OracleDataSource)ic.lookup("java:comp/env/jdbc/nalabor");

答案 1 :(得分:0)

我遇到了这个问题并且我解决了。注意web.xml中元素的顺序。我按照http://wiki.apache.org/tomcat/Specifications所说的方式在我的网络应用程序的web.xml中使用元素的顺序,并且它有效。