This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Servlets and the fly likes Mutiple instances of request handlers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Mutiple instances of request handlers" Watch "Mutiple instances of request handlers" New topic
Author

Mutiple instances of request handlers

Anupreet Arora
Ranch Hand

Joined: Jun 17, 2003
Posts: 81
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

Joined: Mar 22, 2000
Posts: 12769
    
    5
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Mutiple instances of request handlers