Craig,
My parent action class has private instance declarations.
The central one is my "master" bean, the one that essentially
controls (and contains a reference to) all the business beans
in the session.

I think my problem then is as you stated it. Because I am caching
the action class instances there is only ever one instance of any
given action class type. Therefore it is possible that a user
request could be issued to the controller servlet and be assigned
an action object instance that is currently being used by another
user, and therefore it gets a copy of the wrong "master bean."

Does this mean that I should move this parent instance declaration
down into local space in each of my (many) action leaf classes?
This would require a lot of inelegant boilerplate code to access
the master bean in the local action class perform() methods. Ugh.

Is there any other way around this? What about making the parent
declarations synchronized? Or maybe abandoning the caching
of action objects - new instance per request. Just ideas.
Performance issues on both mitigate against I suspect.

Thanks,
Ferghil


-----Original Message-----
From: A mailing list about Java Server Pages specification and reference
[mailto:[EMAIL PROTECTED]]On Behalf Of Craig R. McClanahan
Sent: Thursday, August 24, 2000 1:55 PM
To: [EMAIL PROTECTED]
Subject: Re: Model 2 and large applications - and request
dispatcherthread safety


Ferghil O'Rourke wrote:

> [snip]
> So, on the whole my problems with MVC have not been performance related.
> However, I am experiencing threadsafe problems I think due to my use of
the
> request dispatcher - as mentioned just now by Tim.
> Under heavy concurrent loads I experience data integrity problems.
> Sometimes - and it's very difficult to reproduce - a user will be served
> a page that is clearly one that should have been submitted to another
user.
>

This sounds like a bug in your servlet container, or a thread safety issue
in your
action classes (see more below).

>
> What I'm pretty sure this is, is that there is no guarantee that an object
> I send onto the Req Dispatcher's queue will be necessarily pulled off by
the
> JSP it is meant for. Right now I'm not entirely sure how to ensure that
> the object returned from a call to request.getAttribute("BusinessBean") at
> the top
> of a JSP is the one that belongs to this user's session. What exactly is
the
> relationship between the request dispatcher and the session? Are they
> totally
> independent of each other? If the request dispatcher should operate within
> the
> context of a session (which I'm beginning to realize I had been assuming)
> then
> there may be some issue with my app server (Orion 1.1.37 - a great product
> btw)
> and it's managment of the request dispatcher queue.
>

The request.getAttribute() call gets a *request* attribute, not a *session*
attribute.  Action classes, and the JSP pages you forward to, have access to
both
kinds -- you need to make sure that both pieces of logic agree whether a
particular
item will be passed one way or the other.  But, programmed correctly, the
servlet
container's request dispatcher implementation will always forward the
correct
request (and therefore the correct session) to the correct JSP page.

The most important thing to keep in mind, however, is that the developer of
action
procedures *must* be aware that multiple requests to the same action might
be
happening simultaneously, for the same or different users.  This is the same
basic
rule that you need to be aware of when programming regular servlets -- there
is
only one instance.

Where this matters is how you declare and use variables.  If you declare
variables
locally inside a method, there is a copy of that variable on the stack for
each
simultaneous request, and they will not interfere with each other.  However,
if you
declare a variable outside the scope of any method, there will be only *one*
copy
of that variable.  Trying to use such a variable to hold information that is
specific to a particular request is a recipie for the kinds of problems you
observe, because the value will get scribbled on by other requests that are
going
on at the same time.

To avoid this, I make it a rule to *never ever* use instance variables in an
action
class.  In a servlet, I only use them to store read-only copies of stuff
that is
set up in the init() method and then stays constant.  Following this simple
rule
will avoid a large percentage of the potential multi-thread related problems
you
might have.  It might be worth a quick scan through your source code base,
to see
if someone might have accidentally used an instance variable instead of a
local
one.

>
> On the whole I've found model 2 a very efficient development paradigm.
> But I do have concerns over thread safety.
> Any help/comments appreciated
> Thanks,
> Ferghil O'Rourke
> USMoving Inc.
>

Craig McClanahan

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:

 http://java.sun.com/products/jsp/faq.html
 http://www.esperanto.org.nz/jsp/jspfaq.html
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
 http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets

Reply via email to