struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: Struts Actions are Singletons?
Date Wed, 24 Apr 2002 18:25:20 GMT

On Wed, 24 Apr 2002 wrote:

> Date: Wed, 24 Apr 2002 13:43:26 -0400
> From:
> Reply-To: Struts Users Mailing List <>
> To: Struts Users Mailing List <>
> Subject: Re: Struts Actions are Singletons?
> Can someone explain why these are created as singletons in the first place?

Are you speaking of Action instances?  If so, they are created as
singletons for a number of reasons:

* The threading behavior of Actions are exactly the same as those
  of servlets and JSP pages, where it is quite common for several
  requests to be present in your Action at the same time.

* Java's implementation of a stack per thread (which is where the
  local variables go) makes it easy to avoid most threading problems
  by simply using local variables for the per-request information.

* At the same time, it is quite common to have some of your data
  shared across all requests that use a particular Action.  An example
  would be a list of valid values for a particular form bean property --
  you might well choose to read that information into a shared variable
  at the beginning of your application, instead of going to the
  database on every request.  An instance variable inside the action
  (or perhaps a servlet context attribute if you need the list as part
  of your UI also) makes a perfectly reasonable place to store these.

Moving instance variables into the session, by the way, reduces (but does
not eliminate) multiple thread concerns.  You still need to be aware of
multiple requests accessing the same session at the same time.  "How is
that possible?", you might ask -- consider:

* User submits a form to a long running transaction, gets
  impatient, presses STOP, and goes somewhere else in your
  app (and therefore in the same session) before the first
  request has completed.

* You are using frames -- most browsers will submit multiple
  simultaneous requests that will typically be accessing the
  same session.


To unsubscribe, e-mail:   <>
For additional commands, e-mail: <>

View raw message