users@glassfish.java.net

Problem with multithreaded lookup in InitialContext.

From: <glassfish_at_javadesktop.org>
Date: Wed, 21 Mar 2007 08:05:22 PST

I've got simple test:

public class Test2
{
        @Test
        public void test() throws NamingException, InterruptedException
        {
                Thread t1 = new TestThread();
                Thread t2 = new TestThread();
                t1.start();
                t2.start();
                t1.join();
                t2.join();

        }

        private class TestThread extends Thread
        {
                public void run()
                {
                        try
                        {
                                InitialContext ctx = new InitialContext();
                                ctx.lookup(SLSBA.class.getCanonicalName()); // here is EXCEPTION

                        }
                        catch (NamingException e)
                        {
                                throw new RuntimeException(e);
                        }
                }
        }
}

When trying to make lookup on InitialContext I've got such exception:

Exception in thread "Thread-1" java.lang.RuntimeException: javax.naming.NamingException: ejb ref resolution error for remote business interfaceru.tests.ejb.sf.SLSBA [Root exception is java.lang.RuntimeException: Could not invoke defineClass!]
        at ru.tests.ejb.test.Test2$TestThread.run(Test2.java:42)
Caused by: javax.naming.NamingException: ejb ref resolution error for remote business interfaceru.tests.ejb.sf.SLSBA [Root exception is java.lang.RuntimeException: Could not invoke defineClass!]
        at com.sun.ejb.EJBUtils.lookupRemote30BusinessObject(EJBUtils.java:348)
        at com.sun.ejb.containers.RemoteBusinessObjectFactory.getObjectInstance(RemoteBusinessObjectFactory.java:61)
        at javax.naming.spi.NamingManager.getObjectInstance(NamingManager.java:304)
        at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:320)
        at javax.naming.InitialContext.lookup(InitialContext.java:351)
        at ru.tests.ejb.test.Test2$TestThread.run(Test2.java:37)
Caused by: java.lang.RuntimeException: Could not invoke defineClass!
        at com.sun.corba.ee.impl.codegen.CodeGeneratorUtil.makeClass(CodeGeneratorUtil.java:69)
        at com.sun.corba.ee.spi.codegen.Wrapper._generate(Wrapper.java:933)
        at com.sun.ejb.EJBUtils$2.run(EJBUtils.java:505)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.ejb.EJBUtils.generateAndLoad(EJBUtils.java:502)
        at com.sun.ejb.EJBUtils.loadGeneratedGenericEJBHomeClass(EJBUtils.java:439)
        at com.sun.ejb.EJBUtils.lookupRemote30BusinessObject(EJBUtils.java:321)
        ... 5 more
Caused by: java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:585)
        at com.sun.corba.ee.impl.codegen.CodeGeneratorUtil.makeClass(CodeGeneratorUtil.java:66)
        ... 11 more
Caused by: java.lang.LinkageError: duplicate class definition: com/sun/ejb/codegen/GenericEJBHome_Generated
        at java.lang.ClassLoader.defineClass1(Native Method)
        at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
        ... 16 more

Using one InitialContext and synchronizing it among the threads helps to resolve this situation.

But according to InitialContext javadocs:

"An InitialContext instance is not synchronized against concurrent access by multiple threads. Multiple threads each manipulating a different InitialContext instance need not synchronize. Threads that need to access a single InitialContext instance concurrently should synchronize amongst themselves and provide the necessary locking."

there is nothing criminal in this test and no need to make any sync.

Any thoughts?
[Message sent by forum member 'hazurek' (hazurek)]

http://forums.java.net/jive/thread.jspa?messageID=209230