This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts 1 vs Struts 2 - singleton vs non-singleton

 
Swerrgy Smith
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I've read the following difference between Struts 1 and Struts 2:

Struts 1 Actions are singletons and must be thread-safe since there will only be one instance of a class to handle all requests for that Action. The singleton strategy places restrictions on what can be done with Struts 1 Actions and requires extra care to develop. Action resources must be thread-safe or synchronized.

Struts 2 Action objects are instantiated for each request, so there are no thread-safety issues. (In practice, servlet containers generate many throw-away objects per request, and one more object does not impose a performance penalty or impact garbage collection.)


I know that a singleton class is a class that can have only one instance. But I still do not understand the true nature of the difference between these two versions of Struts when using singleton and non-singleton strategies. Could you please tell me more about this and give me an example?

Thank you.
 
Swerrgy Smith
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone help me?
 
Joe Ess
Bartender
Pie
Posts: 9259
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what you are looking for here. Struts 1 and 2 have different approaches to actions. Since Struts 1 only creates one instance of a particular action, the programmer must always keep thread safety in mind. For example, if one uses an instance variable in an action, it can create a race condition as multiple threads each set the variable to their own particular value.
Since Struts 2 creates an action instance for each request, this is one less thing the developer has to worry about.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic