wood burning stoves 2.0*
The moose likes Servlets and the fly likes Thread safety and servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Thread safety and servlets" Watch "Thread safety and servlets" New topic
Author

Thread safety and servlets

tom walters
Ranch Hand

Joined: Feb 14, 2002
Posts: 34
I always get confused about thread safety and servlets. I know multiple threads call doPost or doGet. here is an example:
public class MyServlet extends HttpServlet
{
privateHelper help;

public void init() throws ServletException
{
help = new Helper();
}

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException
{
help.helperMethod(req);
}
}
Helper is an instance variable of the servlet (which usually means not thread safe) but here the Helper class does not store state, the methods get the state needed passed in. Is this thread safe?
would it be better if i removed the class variable help and inside doPost i did
new Helper.helperMethod(req);
how about a static method instead:
Helper.helperMethod(req);
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
Does Helper have any instance variables that change between users? If Helper only uses local variables then you are safe. No reason to make a new one unless there is a problem with instance variables. Static would have the same effect as your single member - seems to me.
Bill
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

While a servlet container "shouldn't" create more than one instance, I'm not sure if the spec actually says MUSTN'T. Thus, a static (class-scope) object is safer. It's a good habit to form, anyway.
For pure logic helper classes (no member variables), you can dispense with having an instance altogether - just use class methods, and that's safer still.


Customer surveys are for companies who didn't pay proper attention to begin with.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16019
    
  20

AHA! Yes, there IS a situation where multiple instances of the same servlet can exist, and the part of the spec that tells when and how is quoted here: http://www.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=7&t=007646
Which validates what I said above.
Kyle Brown
author
Ranch Hand

Joined: Aug 10, 2001
Posts: 3892
    
    5
Originally posted by Tim Holloway:
While a servlet container "shouldn't" create more than one instance, I'm not sure if the spec actually says MUSTN'T. Thus, a static (class-scope) object is safer. It's a good habit to form, anyway.
For pure logic helper classes (no member variables), you can dispense with having an instance altogether - just use class methods, and that's safer still.

Actually there are a lot of reasons to use instance methods in a stateless helper object. Foremost among these is that you can subclass your helper class and override the methods -- something you can't do with class methods.
Kyle


Kyle Brown, Author of Persistence in the Enterprise and Enterprise Java Programming with IBM Websphere, 2nd Edition
See my homepage at http://www.kyle-brown.com/ for other WebSphere information.
Steve Granton
Ranch Hand

Joined: Jan 13, 2002
Posts: 200
Hello,
Why does the servlet need to keep a reference to the helper class as an instance variable?
Why not just create a new local helper class in the method that it is needed?
If you are concerned that creating new helper objects when needed will have a performance hit then you could always have a pool of helper objects that would then be available to all servlets rather than just tying the object to a single servlet.
Thanks,
Steve
Gerry Giese
Ranch Hand

Joined: Aug 02, 2001
Posts: 247
I think everyone here caught a part of it. I believe we have three different situations where a helper class may be used, and therefore several ways (patterns?) of implementing a solution. Anyone/everyone please correct me if I'm slightly misguided (or just flippin' wrong!).
Case 1
---------
Helper is doing processing related to all users of the system.
Example: Hit counter.
Solution: Use a Helper with a static member variable, and make the method used to change the variable synchronized in order to make it safe and produce correct results. An optional implementation is rather than making the Helper a member variable of the servlet, create it in the init() but place it in the ServletContext, and retrieve it in the doXXX processing. This would allow the Helper to be accessed by other servlets/JSPs as well.
Case 2
---------
Helper performs simple logic or utility processing on request input.
Example: Form Validation.
Solution: Instantiate a new Helper object in the doXXX method and use it as needed. Do not use static member variables in it.
Case 3
---------
Helper performs complex logic or utility processing on request input that involves creation of other objects to aid processing, or Helper needs to be part of a framework or extensible class hierarchy that supports/enforces certain functionality or the particular Helper do be used is picked dynamically based on request input or session/application state.
Example: Form validation with database update using a logging class and a database connection factory.
Solution: Write a Helper factory that creates and/or pools Helper objects. Additional objects that support the Helper (HelperHelpers?) may also need to be retrieved from a factory depending on requirements.


CJP (Certifiable Java Programmer), AMSE (Anti-Microsoft Software Engineer)
Author of Posts in the Saloon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread safety and servlets