File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes About Servlet theadsafe issue. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "About Servlet theadsafe issue." Watch "About Servlet theadsafe issue." New topic
Author

About Servlet theadsafe issue.

Albert Wong
Greenhorn

Joined: Apr 02, 2002
Posts: 9
Hi everyone,
I got a confused thought about the servlet theadsafe issue,
In the service method, if I use
HttpSession hs = request.getSession();
Object object = hs.getAttribute(attName);
is the local variable "hs" and "object" theadsafe ?

Thanks in advance.
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Objects created in a service method should be Thread safe, because every service methods runs in its own thread.
Albert Wong
Greenhorn

Joined: Apr 02, 2002
Posts: 9
That is why I got confused.
First, the local varibles should be threadsafe,
but on the other hand, the attribute value of the session maybe is changed by the other thread of service. for this thought, it is NOT theadsafe,
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Originally posted by Albert Wong:
That is why I got confused.
but on the other hand, the attribute value of the session maybe is changed by the other thread of service. for this thought, it is NOT theadsafe,

It might be depend on if the object in the attribute value bound to the sessions is accessible by other threads of service.
If the object in the attribute is a class variable or an object member variable you might get into trouble.
tony lee
Ranch Hand

Joined: Jan 21, 2002
Posts: 52
Hi Albert,
I think there's a fine line between a local variable and a session object.
Let's consider the two statement you mentioned:

HttpSession hs = request.getSession();
Object object = hs.getAttribute(attName);

Here, object, hs are local variables. They are threadsafe because they're request-based, meaning them're not accessible by any other threads. Session attribute with name "attName" itself is thread-Unsafe because other request might change its value by using setAttribute(attName). Sure if "attName" is changed afterword, hs & object in the method contain staled information.


SCJP2, SCWCD
Albert Wong
Greenhorn

Joined: Apr 02, 2002
Posts: 9
Thanks for you guys.
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
a session is unique per user. So how access the same session concurrently?
The only idea I have is to open a second browser window, navigate to 2 different forms which processing is each accessing the session attribute object and hit them one after another with a short interval. Under that circumstances a thread-unsafe problem might arise. But its really quite theoretic.
Just thinking loud.
Michael Santosa
Greenhorn

Joined: Mar 13, 2002
Posts: 19
In my opinion "object" instance here "is not
really thread safe"..
The codes are :
HttpSession hs = request.getSession();
Object object = hs.getAttribute(attName);
the object itself is thread safe, since
it's a local variable. But the
value referenced by it, is not thread
safe, isn't ? please correct me if I am wrong.
Lets see this example :
In Servlet A
Vector x = new Vector();
x.add("Test");
HttpSession mysession = request.getSession(true);
mysession.setAttribute("test", x);
Now x is put in the session attribute..
And there is another servlet that get it,
and clear the content of vector.
In Servlet B
HttpSession mysession = request.getSession(true);
Vector temp = (Vector) mysession.getAttribute("test");
temp.clear();
And let's back to the servlet A, there is a
command like this :
x.get(1); .. it whould be in dangerous or ??
It depends on servlet implementation,
is setAttribute method of HttpSession save
the Object by reference or it copys the
value ??
So if there is a question about it, for
example is object thread safe ?
Then what would you answer ?
[ April 04, 2002: Message edited by: Michael Santosa ]
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
Originally posted by Michael Santosa:

It depends on servlet implementation,
is setAttribute method of HttpSession save
the Object by reference or it copys the
value ??

I am sure it does not copy the value.
But the session is bound to o.n.e user. So the user would have to use 2 browser windows to provoke session problems. I am to tired to test it. Maybe weekend.
Peter den Haan
author
Ranch Hand

Joined: Apr 20, 2000
Posts: 3252
Originally posted by Axel Janssen:
a session is unique per user. So how access the same session concurrently? [...] But its really quite theoretic.
Oh boy, that's a lot easier than you'd perhaps think.
  • You might be using frames.
  • There might be page elements - e.g. dynamically rendered graphs - requested in parallel by the browser.
  • Some of your code may take an unexpectedly long time to complete due to server load, making the user impatient.
  • The user may open additional browser windows (I sure do).
  • The user may click the refresh button twice in rapid succession; you'd be surprised how many Windows users routinely double-click everything in sight.
  • It is certainly not mere theory, and Sun does state in the specification that the thread safety of session-scoped objects should be carefully considered. In practice most will do fine without being threadsafe, but you do not want to be bitten unexpectedly by the proverbial exception - especially because it is likely to show up after taking your system into production, when server load gets high.
    Nothing that has session scope is safe from threading issues. That a reference to such an object is stored into a local variable ("hs") is completely irrelevant.
    - Peter
    [ April 04, 2002: Message edited by: Peter den Haan ]
    Axel Janssen
    Ranch Hand

    Joined: Jan 08, 2001
    Posts: 2164
    Thanks Peter.
    Axel
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: About Servlet theadsafe issue.