JNDI lookup in new Thread - Websphere

This is a discussion on JNDI lookup in new Thread - Websphere ; Hi all, I have a problem to lookup JNDI object. If the lookup is done in the thread created by Websphere, everything runs fine. But if the lookup is performed in thread created by our application, lookup fails. See the ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: JNDI lookup in new Thread

  1. JNDI lookup in new Thread

    Hi all,
    I have a problem to lookup JNDI object. If the lookup is done in the thread
    created by Websphere, everything runs fine. But if the lookup is performed
    in thread created by our application, lookup fails. See the configuration,
    test code and results below.

    Thanks for any help
    Vlastik.


    Websphere configuration:
    ---------------------------
    The problem occurs on Websphere 5.1 and also on Websphere 6.0

    BM WebSphere Application Server, 5.1.0
    Build Number: b0344.02
    Build Date: 11/4/03

    IBM WebSphere Application Server - Express, 6.0.0.1
    Build Number: o0445.08
    Build Date: 11/10/04

    Test code
    ----------
    The following code performs JNDI lookup in origin thread and also in new
    thread. I get the same result creating context with map of properties (4
    commented lines)


    private void doJNDILookup (String aComment) {
    try {
    InitialContext tempIC;
    System.out.println("JNDI lookup
    "+aComment + " started");

    tempIC = new InitialContext();

    //java.util.Properties p = new
    java.util.Properties();
    //p.put(Context.PROVIDER_URL,
    "iiop://localhost:901");
    //p.put(Context.INITIAL_CONTEXT_FACTORY,
    "com.ibm.websphere.naming.WsnInitialContextFactory");
    //tempIC = new InitialContext(p);

    DataSource tempDS = (DataSource)
    tempIC.lookup("java:comp/env/jdbc/ValuemationDataSourceRef");

    System.out.println("JNDI lookup
    "+aComment + " succesfull");
    } catch (NamingException e) {
    System.out.println("JNDI lookup
    "+aComment + " failed");
    e.printStackTrace();
    } catch (Throwable e) {
    System.out.println("JNDI lookup
    "+aComment + " failed");
    e.printStackTrace();
    } finally {
    System.out.println("JNDI lookup
    "+aComment + " finished");
    }
    }

    private void testJNDI () {
    doJNDILookup("Test 1");
    doJNDILookup("Test 2");

    Thread myThread = new Thread () {
    /**
    * @see java.lang.Thread#run()
    */
    public void run() {
    doJNDILookup("Test 3");
    }
    };

    myThread.start();
    try {
    Thread.sleep(500);
    } catch (InterruptedException e) {
    }

    }


    Test results
    ------------
    Here is the output written to a log:
    [3/11/05 11:07:34:202 CET] 00000014 SystemOut O JNDI lookup Test 1
    started
    [3/11/05 11:07:34:477 CET] 00000014 SystemOut O JNDI lookup Test 1
    succesfull
    [3/11/05 11:07:34:477 CET] 00000014 SystemOut O JNDI lookup Test 1
    finished
    [3/11/05 11:07:34:477 CET] 00000014 SystemOut O JNDI lookup Test 2
    started
    [3/11/05 11:07:34:486 CET] 00000014 SystemOut O JNDI lookup Test 2
    succesfull
    [3/11/05 11:07:34:487 CET] 00000014 SystemOut O JNDI lookup Test 2
    finished
    [3/11/05 11:07:34:487 CET] 0000002c SystemOut O JNDI lookup Test 3
    started
    [3/11/05 11:07:34:489 CET] 0000002c SystemOut O JNDI lookup Test 3
    failed
    [3/11/05 11:07:34:493 CET] 0000002c SystemOut O JNDI lookup Test 3
    finished

    And here is the exception
    ----------------------------
    [3/11/05 11:07:34:490 CET] 0000002c SystemErr R
    javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context
    "java:".
    at
    com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInt ernal(NameSpace.java:1716)
    at
    com.ibm.ws.naming.ipbase.NameSpace.lookupInternal( NameSpace.java:1062)
    at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpac e.java:985)
    at
    com.ibm.ws.naming.urlbase.UrlContextImpl.lookup(Ur lContextImpl.java:1263)
    at
    com.ibm.ws.naming.java.javaURLContextRoot.lookup(j avaURLContextRoot.java:198
    )
    at
    com.ibm.ws.naming.java.javaURLContextRoot.lookup(j avaURLContextRoot.java:139
    )
    at javax.naming.InitialContext.lookup(InitialContext. java:361)
    at
    de.usu.s3.web.internal.VMWApplication.doJNDILookup (VMWApplication.java:732)
    at
    de.usu.s3.web.internal.VMWApplication.access$0(VMW Application.java:720)
    at
    de.usu.s3.web.internal.VMWApplication$1.run(VMWApp lication.java:754)




  2. Re: JNDI lookup in new Thread

    Hello !
    Creating your own threads in WebSphere is not supported/does not run in
    a J2EE context.
    For WAS 6 have a look at Workmanager - this allows you to start new
    threads with J2EE context.

    Bo Nilsson
    Software Group
    IBM Sweden

    Vlastimil Strak wrote:
    > Hi all,
    > I have a problem to lookup JNDI object. If the lookup is done in the thread
    > created by Websphere, everything runs fine. But if the lookup is performed
    > in thread created by our application, lookup fails. See the configuration,
    > test code and results below.
    >
    > Thanks for any help
    > Vlastik.
    >
    >
    > Websphere configuration:
    > ---------------------------
    > The problem occurs on Websphere 5.1 and also on Websphere 6.0
    >
    > BM WebSphere Application Server, 5.1.0
    > Build Number: b0344.02
    > Build Date: 11/4/03
    >
    > IBM WebSphere Application Server - Express, 6.0.0.1
    > Build Number: o0445.08
    > Build Date: 11/10/04
    >
    > Test code
    > ----------
    > The following code performs JNDI lookup in origin thread and also in new
    > thread. I get the same result creating context with map of properties (4
    > commented lines)
    >
    >
    > private void doJNDILookup (String aComment) {
    > try {
    > InitialContext tempIC;
    > System.out.println("JNDI lookup
    > "+aComment + " started");
    >
    > tempIC = new InitialContext();
    >
    > //java.util.Properties p = new
    > java.util.Properties();
    > //p.put(Context.PROVIDER_URL,
    > "iiop://localhost:901");
    > //p.put(Context.INITIAL_CONTEXT_FACTORY,
    > "com.ibm.websphere.naming.WsnInitialContextFactory");
    > //tempIC = new InitialContext(p);
    >
    > DataSource tempDS = (DataSource)
    > tempIC.lookup("java:comp/env/jdbc/ValuemationDataSourceRef");
    >
    > System.out.println("JNDI lookup
    > "+aComment + " succesfull");
    > } catch (NamingException e) {
    > System.out.println("JNDI lookup
    > "+aComment + " failed");
    > e.printStackTrace();
    > } catch (Throwable e) {
    > System.out.println("JNDI lookup
    > "+aComment + " failed");
    > e.printStackTrace();
    > } finally {
    > System.out.println("JNDI lookup
    > "+aComment + " finished");
    > }
    > }
    >
    > private void testJNDI () {
    > doJNDILookup("Test 1");
    > doJNDILookup("Test 2");
    >
    > Thread myThread = new Thread () {
    > /**
    > * @see java.lang.Thread#run()
    > */
    > public void run() {
    > doJNDILookup("Test 3");
    > }
    > };
    >
    > myThread.start();
    > try {
    > Thread.sleep(500);
    > } catch (InterruptedException e) {
    > }
    >
    > }
    >
    >
    > Test results
    > ------------
    > Here is the output written to a log:
    > [3/11/05 11:07:34:202 CET] 00000014 SystemOut O JNDI lookup Test 1
    > started
    > [3/11/05 11:07:34:477 CET] 00000014 SystemOut O JNDI lookup Test 1
    > succesfull
    > [3/11/05 11:07:34:477 CET] 00000014 SystemOut O JNDI lookup Test 1
    > finished
    > [3/11/05 11:07:34:477 CET] 00000014 SystemOut O JNDI lookup Test 2
    > started
    > [3/11/05 11:07:34:486 CET] 00000014 SystemOut O JNDI lookup Test 2
    > succesfull
    > [3/11/05 11:07:34:487 CET] 00000014 SystemOut O JNDI lookup Test 2
    > finished
    > [3/11/05 11:07:34:487 CET] 0000002c SystemOut O JNDI lookup Test 3
    > started
    > [3/11/05 11:07:34:489 CET] 0000002c SystemOut O JNDI lookup Test 3
    > failed
    > [3/11/05 11:07:34:493 CET] 0000002c SystemOut O JNDI lookup Test 3
    > finished
    >
    > And here is the exception
    > ----------------------------
    > [3/11/05 11:07:34:490 CET] 0000002c SystemErr R
    > javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context
    > "java:".
    > at
    > com.ibm.ws.naming.ipbase.NameSpace.getParentCtxInt ernal(NameSpace.java:1716)
    > at
    > com.ibm.ws.naming.ipbase.NameSpace.lookupInternal( NameSpace.java:1062)
    > at com.ibm.ws.naming.ipbase.NameSpace.lookup(NameSpac e.java:985)
    > at
    > com.ibm.ws.naming.urlbase.UrlContextImpl.lookup(Ur lContextImpl.java:1263)
    > at
    > com.ibm.ws.naming.java.javaURLContextRoot.lookup(j avaURLContextRoot.java:198
    > )
    > at
    > com.ibm.ws.naming.java.javaURLContextRoot.lookup(j avaURLContextRoot.java:139
    > )
    > at javax.naming.InitialContext.lookup(InitialContext. java:361)
    > at
    > de.usu.s3.web.internal.VMWApplication.doJNDILookup (VMWApplication.java:732)
    > at
    > de.usu.s3.web.internal.VMWApplication.access$0(VMW Application.java:720)
    > at
    > de.usu.s3.web.internal.VMWApplication$1.run(VMWApp lication.java:754)
    >
    >
    >


  3. Re: JNDI lookup in new Thread

    Vlastimil Strak wrote:

    > Hi all,
    > I have a problem to lookup JNDI object. If the lookup is done in the thread
    > created by Websphere, everything runs fine. But if the lookup is performed
    > in thread created by our application, lookup fails. See the configuration,
    > test code and results below.



    Correct, this is how it is supposed to work.


  4. Re: JNDI lookup in new Thread

    Vlastimil Strak wrote:

    >Hi all,
    >I have a problem to lookup JNDI object. If the lookup is done in the thread
    >created by Websphere, everything runs fine. But if the lookup is performed
    >in thread created by our application, lookup fails. See the configuration,
    >test code and results below.
    >
    >Thanks for any help
    >Vlastik.
    >
    >


    Your thread is not running in a container, so there's no local context.
    You'll have to look up the 'global' JNDI name instead of your reference.

    Ken

+ Reply to Thread