Topic
  • 9 replies
  • Latest Post - ‏2013-03-29T15:25:55Z by Jacek_Laskowski
SystemAdmin
SystemAdmin
590 Posts

Pinned topic JNDI - No Initial Context

‏2013-03-28T06:40:11Z |
Hello,

We are using WebSphere Liberty Profile. We are getting this exception(see attached console.log) when we try to initialize the datasource. We have configured the server.xml with datasource and the web.xml. Also using jndi-1.0 and transaction-1.1 features.
Is there a way to debug this error ?
javax.naming.NoInitialContextException: The calling code's BundleContext could not be determined.
  • frowe
    frowe
    7 Posts

    Re: JNDI - No Initial Context

    ‏2013-03-28T19:45:11Z  
    Please provide a bit more context for the problem, like how your web app is packaged, what the data source definition in server.xml and web.xml looks like, etc

    Fred
  • Jacek_Laskowski
    Jacek_Laskowski
    133 Posts

    Re: JNDI - No Initial Context

    ‏2013-03-28T21:01:30Z  
    • frowe
    • ‏2013-03-28T19:45:11Z
    Please provide a bit more context for the problem, like how your web app is packaged, what the data source definition in server.xml and web.xml looks like, etc

    Fred
    Hi,

    I've seen the error message before, but can't remember where it was and what led to the exception.

    In addition to what Fred asked for I'd also like to see the code snippet where the initial context is instantiated and then executed with lookup.

    Jacek
    Japila :: verba docent, exempla trahunt
  • SystemAdmin
    SystemAdmin
    590 Posts

    Re: JNDI - No Initial Context

    ‏2013-03-28T21:08:34Z  
    • frowe
    • ‏2013-03-28T19:45:11Z
    Please provide a bit more context for the problem, like how your web app is packaged, what the data source definition in server.xml and web.xml looks like, etc

    Fred
    The webapp is packaged in a war file. Below is the snippet from server.xml.

    <dataSource id="catalog" jndiName="jdbc/catalog" statementCacheSize="20">
    <properties.derby.client createDatabase="create"
    databaseName="${var.home}/derby/catalog"
    password="{xor}PTY+OzI2MQ==" portNumber="1528"
    serverName="xxx.yyy.zzz" user="biadmin"/>
    <jdbcDriver>
    <library>
    <fileset dir="${home}/derby/lib" includes="derbyclient.jar"/>
    </library>
    </jdbcDriver>
    <connectionManager agedTimeout="30m" connectionTimeout="90s"
    maxPoolSize="20" purgePolicy="ValidateAllConnections"/>
    </dataSource>

    Below is the snippet from web.xml
    <resource-ref id="DataSource_Transactional_ResourceRef">
    <res-ref-name>jdbc/catalog</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
  • SystemAdmin
    SystemAdmin
    590 Posts

    Re: JNDI - No Initial Context

    ‏2013-03-28T21:17:07Z  
    Hi,

    I've seen the error message before, but can't remember where it was and what led to the exception.

    In addition to what Fred asked for I'd also like to see the code snippet where the initial context is instantiated and then executed with lookup.

    Jacek
    Japila :: verba docent, exempla trahunt
    the code snippet for initializing the context is like,

    Content c=null;
    try{
    c = new InitialContext();
    DataSource d = (DataSource)d.lookup(jdbc/catalog);
    }catch(NamingException e) {
    throw new RuntimeException("msg" + e);
    }

    Not sure if server.xml and web.xml have been configured. How does this work? I mean if we configure the datasources in server.xml and start the liberty profile, can lookups be done by the application ? when is that these datasources gets registered with JNDI.
  • frowe
    frowe
    7 Posts

    Re: JNDI - No Initial Context

    ‏2013-03-28T22:05:52Z  
    the code snippet for initializing the context is like,

    Content c=null;
    try{
    c = new InitialContext();
    DataSource d = (DataSource)d.lookup(jdbc/catalog);
    }catch(NamingException e) {
    throw new RuntimeException("msg" + e);
    }

    Not sure if server.xml and web.xml have been configured. How does this work? I mean if we configure the datasources in server.xml and start the liberty profile, can lookups be done by the application ? when is that these datasources gets registered with JNDI.
    A correction to your code snippet
    c.lookup(...) instead of d.lookup(...)
    However, that doesn't fix the problem. The root cause of the problem is that the new thread your task is being executed in by the ScheduledExecutorService is unaware of the JEE environment and things like the namespace, classloaders, etc (often referred to as the thread context in a JEE environment). JSR236 will address this problem in JEE7 by providing a JEE-aware environment in which a ManagedSchedulerExecutorService will make those contexts available to your task thread. In the mean time, you can try (and caveat emptor) to lookup the datasource in the main app thread wihch is JEE aware, and somehow make that available to the the task. However, be aware that you may have transaction problems and will definitely not be able to obtain a UserTransaction or other goodies in the task. You can learn more about JSR236 and read the public review draft from the JCP website.

    Fred
  • SystemAdmin
    SystemAdmin
    590 Posts

    Re: JNDI - No Initial Context

    ‏2013-03-28T22:25:17Z  
    • frowe
    • ‏2013-03-28T22:05:52Z
    A correction to your code snippet
    c.lookup(...) instead of d.lookup(...)
    However, that doesn't fix the problem. The root cause of the problem is that the new thread your task is being executed in by the ScheduledExecutorService is unaware of the JEE environment and things like the namespace, classloaders, etc (often referred to as the thread context in a JEE environment). JSR236 will address this problem in JEE7 by providing a JEE-aware environment in which a ManagedSchedulerExecutorService will make those contexts available to your task thread. In the mean time, you can try (and caveat emptor) to lookup the datasource in the main app thread wihch is JEE aware, and somehow make that available to the the task. However, be aware that you may have transaction problems and will definitely not be able to obtain a UserTransaction or other goodies in the task. You can learn more about JSR236 and read the public review draft from the JCP website.

    Fred
    Hi,

    I believe the problem you are seeing was fixed in PM70625 [1] which is available via fix central. You will need to be entitled to support in order to download this fix.

    Thanks
    Alasdair

    [1] http://www.ibm.com/support/fixcentral/swg/quickorder?parent=ibm~WebSphere&product=ibm/WebSphere/WebSphere+Application+Server&release=8.5.0.1&platform=Linux&function=fixId&fixids=8.5.0.1-WS-WASProd_WLPArchive-IFPM70625&includeRequisites=1&includeSupersedes=0&downloadMethod=http&source=fc
  • SystemAdmin
    SystemAdmin
    590 Posts

    Re: JNDI - No Initial Context

    ‏2013-03-29T00:04:53Z  
    Hi,

    I believe the problem you are seeing was fixed in PM70625 [1] which is available via fix central. You will need to be entitled to support in order to download this fix.

    Thanks
    Alasdair

    [1] http://www.ibm.com/support/fixcentral/swg/quickorder?parent=ibm~WebSphere&product=ibm/WebSphere/WebSphere+Application+Server&release=8.5.0.1&platform=Linux&function=fixId&fixids=8.5.0.1-WS-WASProd_WLPArchive-IFPM70625&includeRequisites=1&includeSupersedes=0&downloadMethod=http&source=fc
    Hello,

    Thanks for your response guys. So, I changed my application to get
    the datasource in the main thread and passed the datasource as an
    argument to the other threads outside container and so far looks
    like its working. I will keep posted if things change. thanks!

    Sree.
  • Jacek_Laskowski
    Jacek_Laskowski
    133 Posts

    Re: JNDI - No Initial Context

    ‏2013-03-29T15:14:17Z  
    Hello,

    Thanks for your response guys. So, I changed my application to get
    the datasource in the main thread and passed the datasource as an
    argument to the other threads outside container and so far looks
    like its working. I will keep posted if things change. thanks!

    Sree.
    Hi,

    Ah, so you've been trying to look up resource references from some other, non-managed threads? That would explain why the stack didn't hold necessary structures (thread locals?) and the code ultimately blew up.

    Jacek
    Japila :: verba docent, exempla trahunt
  • Jacek_Laskowski
    Jacek_Laskowski
    133 Posts

    Re: JNDI - No Initial Context

    ‏2013-03-29T15:25:55Z  
    Hi,

    I believe the problem you are seeing was fixed in PM70625 [1] which is available via fix central. You will need to be entitled to support in order to download this fix.

    Thanks
    Alasdair

    [1] http://www.ibm.com/support/fixcentral/swg/quickorder?parent=ibm~WebSphere&product=ibm/WebSphere/WebSphere+Application+Server&release=8.5.0.1&platform=Linux&function=fixId&fixids=8.5.0.1-WS-WASProd_WLPArchive-IFPM70625&includeRequisites=1&includeSupersedes=0&downloadMethod=http&source=fc
    Hi Alasdair,

    Thanks for the pointer. I found the description of the fix you pointed out at PM70625; 8.5.0.1: NoInitialContextException for InitialContext implementations.

    What I've been struggling with was the sentence:

    "In order for other JNDI context providers to be used the classes for them needed to be available to the
    JNDI implementation used in the Liberty profile."

    How could I have a non-WLP implementation of JNDI InitialContext used in my code and WLP wouldn't know about it? When would I have to keep the jar(s) with the classes so it's visible to an application but not WLP?

    Would you mind elaborating a bit more on the topic?

    Jacek
    Japila :: verba docent, exempla trahunt