• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Mutiple instances of request handlers

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
------------------------------------------------------------
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Do you want ants? Because that's how you get ants. And a tiny ads:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic