Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mutiple instances of request handlers

 
Anupreet Arora
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I posted this question in the Struts forum, but nobody responded. Hoping for some replies in the Servlet forum, as it is much more active.

In essence the Q is that why do we have or why is it recommended to have only one instance of a request handler class ? What is the impact of having a new class to handle each request
Hi,
As per the the Struts documentation for an Action Class, only one instance of the class is created and so, the design of the class should ensure that it is thread-safe.
My Question is what will happen if I change the struts source code, so that the action servlet does not look for an existing instance to handle the incoming request, but creates a new instance for each request? What can/cannot go wrong if this the case? And what can be the performance implications in this scenario?
Best Regards,
Anupreet Arora
PS: I am pasting the section from the Struts documentation which refers to the issue, for the covenience of the readers
-----------------------------------------------------------
An Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request. The controller (RequestProcessor) will select an appropriate Action for each request, create an instance (if necessary), and call the execute method.
Actions must be programmed in a thread-safe manner, because the controller will share the same instance for multiple simultaneous requests. This means you should design with the following items in mind:
Instance and static variables MUST NOT be used to store information related to the state of a particular request. They MAY be used to share global resources across requests for the same action.
Access to other resources (JavaBeans, session variables, etc.) MUST be synchronized if those resources require protection. (Generally, however, resource classes should be designed to provide their own protection where necessary
------------------------------------------------------------
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My Question is what will happen if I change the struts source code, so that the action servlet does not look for an existing instance to handle the incoming request, but creates a new instance for each request? What can/cannot go wrong if this the case? And what can be the performance implications in this scenario?

There would be some performance hit for the object creation and subsequent GC, but newer JVMs are much more efficient at this. On the plus side, your code for a "one instance per request" design could be simpler and possibly faster.
I recently ran into this rather nice discussion about performance for this style of programming that I heartily recommend.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic