You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Weigen Liang <we...@etix.com> on 2003/05/05 07:49:14 UTC

why only one instance of Action class (Action class thread safety)

The current Struts framework creates only one instance for each Action
class, and uses that instance to serve all the relevant requests. This
makes the Action class thread safety a big issue for newer users of the
Struts framework. The struts user mailing list has many, many such
questions from Struts users. This is similar to servlet itself. The
servlet spec has the SingleThreadModel to "try" to solve this problem,
but as we know, this does not really work. 

If one instance of Action object is created for EACH request, then we
would not have this threading issue. This instance would be tied to a
request object and a response object, and has life span of that
particular request. The class would be like this:

public class Action {
   public Action(ActionMapping mapping, ActionForm form, 
      ServletRequest request, ServletResponse response) {
      ...
   }

   public ActionForward execute() {
      ...
   }

   private ActionMapping mapping;
   private ActionForm form;
   private ServletRequest request;
   private ServletResponse response;

   // plus other instance variable for this particular request
   ...
}


Considering that a significant percentage of Struts users have limited
understanding of thread safety, removal of such a concern from the
framework would certainly help the users. 


Weigen





---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org


Re: why only one instance of Action class (Action class thread safety)

Posted by "Craig R. McClanahan" <cr...@apache.org>.
On Sun, 5 May 2003, Weigen Liang wrote:

> Date: 05 May 2003 01:49:14 -0400
> From: Weigen Liang <we...@etix.com>
> Reply-To: Struts Developers List <st...@jakarta.apache.org>
> To: struts-dev@jakarta.apache.org
> Cc: weigen@etix.com
> Subject: why only one instance of Action class (Action class thread
>     safety)
>
> The current Struts framework creates only one instance for each Action
> class, and uses that instance to serve all the relevant requests. This
> makes the Action class thread safety a big issue for newer users of the
> Struts framework. The struts user mailing list has many, many such
> questions from Struts users. This is similar to servlet itself. The
> servlet spec has the SingleThreadModel to "try" to solve this problem,
> but as we know, this does not really work.
>
> If one instance of Action object is created for EACH request, then we
> would not have this threading issue. This instance would be tied to a
> request object and a response object, and has life span of that
> particular request. The class would be like this:
>
> public class Action {
>    public Action(ActionMapping mapping, ActionForm form,
>       ServletRequest request, ServletResponse response) {
>       ...
>    }
>
>    public ActionForward execute() {
>       ...
>    }
>
>    private ActionMapping mapping;
>    private ActionForm form;
>    private ServletRequest request;
>    private ServletResponse response;
>
>    // plus other instance variable for this particular request
>    ...
> }
>

Changing this now would be a major backwards incompatibility issue, so it
will not be done in any Struts 1.x release.  We can look at other
approaches in a 2.x time frame.

Per-request object instantiation is cheaper in 1.4 JDKs, but it's still
not free.  However, lots of Struts users still run on 1.2 and 1.3 JDKs
where this is still a pretty big issue.

>
> Considering that a significant percentage of Struts users have limited
> understanding of thread safety, removal of such a concern from the
> framework would certainly help the users.
>

Yet the large majority of those people seem to figure it out after a while
-- especially the single most important rule (do not store
per-request state information in instance variables of an Action).  As
others have pointed out, the Servlet API imposes exactly the same sort of
issue; it wasn't invented here.

>
> Weigen
>

Craig

---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org


Re: why only one instance of Action class (Action class thread safety)

Posted by Ted Husted <hu...@apache.org>.
In the beginning, object creates were expensive, and the framework was 
being parsimonious. Using Action this way also made it easy to refactor 
working servlets into an Action. But, those days are past.

Moving forward, IMHO, the framework should support a wider range of 
action paradigms. We should also support creating actions using scripts, 
rather than just Java classes. JPublish supports this directly, and 
there is already a Struts extension along these lines.

This sounds scary, but there's really no reason why JSPs could not be 
used as actions (so long as they did not contain any display code).

The use case for scripts and JSPs would be to open the framework to 
non-Java programmers. Right now, to build a Struts web app, there's a 
lot of overhead involved just in getting a Java compiler, Ant, and an 
editor/IDE up and running.

Another approach entirely would be to combine ActionForm and Actions 
into a single object that would collect and validate its own data and 
then execute itself. The use case here would be to permit better object 
orientation to complex applications.

Now, I'm not suggesting that the current approach be modified. I'm 
suggesting that we increase the number of alternative approaches.

I think the trick to making this sort of thing work lies in defining our 
use cases and designing the configuration file to suit our definitions. 
We do have a very good design now, but for Struts 2.0, we might look at 
making it more generic and allowing for a broader range of functionality.

I've posted some ideas I've been kicking around for awhile on the Wiki.

http://nagoya.apache.org/wiki/apachewiki.cgi?StrutsUseCases

http://nagoya.apache.org/wiki/apachewiki.cgi?ModelingNotes

-Ted.


Weigen Liang wrote:
> The current Struts framework creates only one instance for each Action
> class, and uses that instance to serve all the relevant requests. This
> makes the Action class thread safety a big issue for newer users of the
> Struts framework. The struts user mailing list has many, many such
> questions from Struts users. This is similar to servlet itself. The
> servlet spec has the SingleThreadModel to "try" to solve this problem,
> but as we know, this does not really work. 
> 
> If one instance of Action object is created for EACH request, then we
> would not have this threading issue. This instance would be tied to a
> request object and a response object, and has life span of that
> particular request. The class would be like this:
> 
> public class Action {
>    public Action(ActionMapping mapping, ActionForm form, 
>       ServletRequest request, ServletResponse response) {
>       ...
>    }
> 
>    public ActionForward execute() {
>       ...
>    }
> 
>    private ActionMapping mapping;
>    private ActionForm form;
>    private ServletRequest request;
>    private ServletResponse response;
> 
>    // plus other instance variable for this particular request
>    ...
> }
> 
> 
> Considering that a significant percentage of Struts users have limited
> understanding of thread safety, removal of such a concern from the
> framework would certainly help the users. 
> 
> 
> Weigen
> 
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: struts-dev-help@jakarta.apache.org
> 
> 


-- 
Ted Husted,
Struts in Action <http://husted.com/struts/book.html>



---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org


RE: why only one instance of Action class (Action class thread safety)

Posted by Jacob Hookom <ho...@uwec.edu>.
Actions follow the same constraints as the Servlet API such with thread
safety-- see HttpServlet (not to mention that I believe they are getting rid
of the single thread HttpServlet model all together with the API).


| -----Original Message-----
| From: Weigen Liang [mailto:weigen@etix.com]
| Sent: Monday, May 05, 2003 12:49 AM
| To: struts-dev@jakarta.apache.org
| Cc: weigen@etix.com
| Subject: why only one instance of Action class (Action class thread
| safety)
| 
| The current Struts framework creates only one instance for each Action
| class, and uses that instance to serve all the relevant requests. This
| makes the Action class thread safety a big issue for newer users of the
| Struts framework. The struts user mailing list has many, many such
| questions from Struts users. This is similar to servlet itself. The
| servlet spec has the SingleThreadModel to "try" to solve this problem,
| but as we know, this does not really work.
| 
| If one instance of Action object is created for EACH request, then we
| would not have this threading issue. This instance would be tied to a
| request object and a response object, and has life span of that
| particular request. The class would be like this:
| 
| public class Action {
|    public Action(ActionMapping mapping, ActionForm form,
|       ServletRequest request, ServletResponse response) {
|       ...
|    }
| 
|    public ActionForward execute() {
|       ...
|    }
| 
|    private ActionMapping mapping;
|    private ActionForm form;
|    private ServletRequest request;
|    private ServletResponse response;
| 
|    // plus other instance variable for this particular request
|    ...
| }
| 
| 
| Considering that a significant percentage of Struts users have limited
| understanding of thread safety, removal of such a concern from the
| framework would certainly help the users.
| 
| 
| Weigen
| 
| 
| 
| 
| 
| ---------------------------------------------------------------------
| To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
| For additional commands, e-mail: struts-dev-help@jakarta.apache.org
| 
| ---
| Incoming mail is certified Virus Free.
| Checked by AVG anti-virus system (http://www.grisoft.com).
| Version: 6.0.476 / Virus Database: 273 - Release Date: 4/24/2003
| 

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.476 / Virus Database: 273 - Release Date: 4/24/2003
 


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org


RE: why only one instance of Action class (Action class thread safety)

Posted by Andrew Hill <an...@gridnode.com>.
The main argument against this is that it is inefficient and will result in
more object creation - taking time and memory.

Personally I dont think that one extra object per request would be such a
big deal - I mean its already created an action form instance, gone through
all the effort of poulating it etc... One more new Action object probably
wouldnt hurt too much... (and its pretty insignificant compared to all the
tons of objects that get created and garbage collected inside one of my
actions!...)

And like you point out in your example, it would make the signature a lot
simpler for the execute method. Furthermore one could add extra setters for
custom stuff that could be called from a custom request processor.

That said - the efficiency argument does still stand - and I cant see the
struts developers being ready to make such a big break with the current
design. There is also a certain truth in the argument that things are
simplified for developers by following the same technique as the servlet api
itself.

-----Original Message-----
From: Weigen Liang [mailto:weigen@etix.com]
Sent: Monday, 5 May 2003 13:49
To: struts-dev@jakarta.apache.org
Cc: weigen@etix.com
Subject: why only one instance of Action class (Action class thread
safety)


The current Struts framework creates only one instance for each Action
class, and uses that instance to serve all the relevant requests. This
makes the Action class thread safety a big issue for newer users of the
Struts framework. The struts user mailing list has many, many such
questions from Struts users. This is similar to servlet itself. The
servlet spec has the SingleThreadModel to "try" to solve this problem,
but as we know, this does not really work.

If one instance of Action object is created for EACH request, then we
would not have this threading issue. This instance would be tied to a
request object and a response object, and has life span of that
particular request. The class would be like this:

public class Action {
   public Action(ActionMapping mapping, ActionForm form,
      ServletRequest request, ServletResponse response) {
      ...
   }

   public ActionForward execute() {
      ...
   }

   private ActionMapping mapping;
   private ActionForm form;
   private ServletRequest request;
   private ServletResponse response;

   // plus other instance variable for this particular request
   ...
}


Considering that a significant percentage of Struts users have limited
understanding of thread safety, removal of such a concern from the
framework would certainly help the users.


Weigen





---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: struts-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-dev-help@jakarta.apache.org