Rich,

You can face some multi-thread problem in your example, because the proxy to
the Home is not multi-thread safe, and the proxy is accessed at the same
time by multiple threads to call the method create().

So, what you can do is to add a synchronized smart proxy:

// FooHome synchronizer (smart proxy)
class SynchFooHome implements FooHome {
        private FooHome _delegate;

        SynchFooHome(String jndi) throws Exception {
                _delegate = Lookup the Home (jndi);
        }

        public synchronized Foo create(int i) {
                return _delegate.create(i);
        }
        ... etc for the other methods ...
}

Then, to get a proxy to a Home:

class HomeManager {
        // To use load balancing, you must disable the cache
        private static boolean  _loadBalance = false;

        // primitive cache (using a Map should be better)
        private static FooHome  _fooHome;

        public static FooHome getFooHome(String jndi) throws Exception {
                FooHome result;

                if( loadBalance )
                        // Do not create the smart proxy, 1 proxy per thread
                        result = Lookup the Home (jndi);
                else {
                        synchronized(HomeManager.class) {
                                if( _fooHome == null )
                                        _fooHome = new SynchFooHome(jndi);
                                result = _fooHome;
                        }
                }
                return result;
        }
        ...
}


Then, your code should works:
> FooHome home = HomeManager.getFooHome("EJBFooHome");
>
>  for( int i = 0; i < 100; i++ )
>  {
>      new Thread() {
>       public void run()
>       {
>            Foo f = home.create( i );
>       }
>   }.start();
>  }

You can create a generic Home manager using a HashMap   and dynamic class
loader to create the smart proxy.
In term of design, it is important to encapsulate the Home proxy policy in a
single class.

This is what I understood from this dicussion. I did not checked the code,
just typing-in in the mail....

Cheers,
Tibo.

> Thanks Tibo, but through no fault of yours I'm still a little fuzzy.
>
> Let me ask this another way. If a client looks up an EJBHome
> from JNDI,
> can that client give that home to multiple concurrent threads?
>
> EJBFooHome home = jndiLookup("EJBFooHome");
>
>  for( int i = 0; i < 100; i++ )
>  {
>      new Thread() {
>       public void run()
>       {
>            Foo f = home.create( i );
>       }
>   }.start();
>  }
>
> In the above case, would the ejb container, which I assume is
> intercepting the create calls against
> home, taking care of synchronization?
>
> What I'm driving at here is this. If the above scenario won't
> work, then I can't cache EJBHome
> object and dole it out to multiple concurrent threads. Right?
>
> Thanks. (Sorry, I keep waiting for the light to go on)
>
> Thibault Cuvillier wrote:
>
> > Rich,
> >
> > I did not found anywhere in the J2EE/RMI spec that a proxy has to be
> > multi-thread safe. I speak about the PROXY, not the remote
> object (which is
> > multi-thread safe according to the J2EE spec). This point
> was confirmed by
> > Chris Raber who is a trustable guy !
> >
> > So, if you share a proxy between multiple threads in a
> client, you must
> > synchronize the proxy by wraping it in a synchronized smart proxy.
> >
> > The Synchronized smart proxy and the linked proxy can be
> stored in a cache
> > to avoid to many JNDI calls.
> >
> > You can face this situation if a servlet/JSP cache the
> proxy to the Home
> > objects, because each user request will start a new thread.
> Most of the
> > systems do not synchronize the cached proxies on a Home,
> and it looks to
> > work anyway.....
> >
> > If more than one client get a proxy to the remote object,
> there is no
> > problem, because each client will use it's own proxy
> instance and server
> > connection.
> >
> > This is what I understood from this discussion.
> >
> > Tibo.
> >
> > > -----Original Message-----
> > > From: Rich Johns [mailto:[EMAIL PROTECTED]]
> > > Sent: Monday, August 14, 2000 3:17 PM
> > > To: [EMAIL PROTECTED]
> > > Subject: Re: Is a Proxy is thread-safe? - Summary
> > >
> > >
> > > Hi Tibo,
> > >
> > > What is the conclusion of this question? Is it really true,
> > > that if you
> > > cache EJBHome objects, you must synchronize them? I thought
> > > the ejb container handled that?
> > >
> > > If multiple clients grab the same EJBHome object from JNDI and
> > > all use it simulataneously, the container must be at work managing
> > > thread saftey. Or, is it the case that each client gets
> its own stub,
> > > which is what allows the thread saftey.
> > >
> > > If you have an understanding of this, could you please
> explain this.
> > > I've just completed a design where I was planning to
> cache EJBHomes
> > > and now I'm not sure.
> > >
> > > thanks very much.
> > >
> >
> > [cut to save bandwidth !]
> >
> >
> ==============================================================
> =============
> > To unsubscribe, send email to [EMAIL PROTECTED] and
> include in the body
> > of the message "signoff EJB-INTEREST".  For general help,
> send email to
> > [EMAIL PROTECTED] and include in the body of the message "help".
>
> ==============================================================
> =============
> To unsubscribe, send email to [EMAIL PROTECTED] and
> include in the body
> of the message "signoff EJB-INTEREST".  For general help,
> send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to