javax.naming.NamingException: Cannot create resource instance

17,457

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+):

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

or this for Java 5 and below:

InitialContext ic = new InitialContext();
OracleDataSource ods = (OracleDataSource)ic.lookup("java:comp/env/jdbc/nalabor");
Share:
17,457
jeff
Author by

jeff

Updated on June 04, 2022

Comments

  • jeff
    jeff almost 2 years

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

    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>
    

    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
    
  • jeff
    jeff about 12 years
    I put the resource in web.xml because that is something I read in a few places including on BalusC's blog here balusc.blogspot.com/2008/07/… One more thing. yesterday I had the datasource working locally in eclipse, I was only getting the naming exception when deployed to a testing server. Today I can't even get it to work in eclipse.
  • jeff
    jeff about 12 years
    yes and no. In Eclipse I got both mine and your code to use a datasource to connect to our Oracle server. Deployed to our testing server neither work. Locally the extra slash had no effect, your code worked with it or without it. I could even put a double slash like this... lookup("java://comp/env/jdbc/navarch").
  • jeff
    jeff about 12 years
    btw, when I removed the resource from the web.xml, I got jdbc not bound in this context, on our testing server anyways. It didn't seem to matter with Eclipse if it was present in the web.xml or not.