• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Regarding SingleThreadModel from J@Whiz

 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If I have a session variable declared inside a service() of a servlet which implements SingleThreadModel,then isnt' that session variable thread-safe(as ALL LOCAL variables are)
viz
public class XYZ implements SingleThreadModel
{
int i=0;//NOT thread safe
public void service(HttpServletRequest req,HttpServletResponse res)
{
HttpSession sess=req.getSession();//Threadsafe or not???
}
}
The answer is given as session is not thread safe.Why it is a local varaible & must be thead safe,isnt it?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there,
U seem to be a bit confused with the session variable. First of all, session object is created by the servlet container and not by you. When u use the statement "HttpSession sess=req.getSession();" u r actually trying to get a reference to an existing variable and not creating a new object.
Hope this clears ur doubt.
Happy Coding...
Fazal.
[ December 17, 2002: Message edited by: fazal ]
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Your variable "int i" is thread safe. since u are implementing the single thread model interface.
Hari
 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is only one copy of the instance variable per instance of the servlet, and as you implements SingleThreadModel, there is always only one thread that execeutes the methods of a servlet instance at a time. So instance variable are thread-safe while you're implementing SingleThreadModel.
Hope it helped you!
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Remember that instance variable are Thread-safe if you're implementing SingleThreadModel, but static variable are not thread-safe, even if you're implementing SingleThreadModel. This is because there exists only one copy of the static variable across all the instances. So even, when Multiple-threads access differents instance of your servlet, they are accessing the same static variable.

I hope it helps!!
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vedhas Pitkar:
The answer is given as session is not thread safe.Why it is a local varaible & must be thead safe,isnt it?


Sorry I haven't read well your question, I let the message that I posted as it may be interesting for other people.
Session variable are not thread-safe. Session variable are accessible only to the threads belonging to that session. But they're may be multiple threads belonging to one session. For example a user may open multiple browser windows and send request. All the request will belong to the same session and they will be able to access the session attributes simultaneously.
I hope this one helped you really
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a tutorial on sun site
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Younes,
I think that if a user opens multiple browser windows, then these will share a session only if cookies are enabled. If not, url rewriting will be used and each browser will represent a different session.
Regards
Swanand
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Swanand Barve:

I think that if a user opens multiple browser windows, then these will share a session only if cookies are enabled. If not, url rewriting will be used and each browser will represent a different session.


I don't think that the statement above is right. Anyone can correct me if I am wrong.
Regards,
Zheng
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, I'm not sure about it. I never thought about it . As far as I know, once you close your browser and open it again, you get a new sessionId. So, IMHO I think that you lose the session and I would agree with Swanand. Hopefully, there is some experts this week that could answer our questions
 
Younes Essouabni
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But the session variable is still not thread safe, since the client may use cookies
 
Vedhas Pitkar
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All agreed,but the session variable is INSIDE the service() & as such a LOCAL variable whose scope finishes as soon as the service() is exited.
I put my question clearly now:
Are any HttpSession or ServletContext variables declared inside any method(local vars) are thread-safe or not?Coz as instance variables they are NOT thread-safe.
Furthur comments,anyone?
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Pitkar,
A servlet that implements SingleThreadModel only guarantees that only one thread in this servlet's instantce at a time will execute.
Any outside object that is referenced by this servlet instance is not thread-safe. It may be accessible at any time to those servlets instances.
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends and pitker,
As far as i know," when servlet implements Single thread model it is same as declaring service method of that servlet as Synchronized ".
So all the local variable in that Service method is Thread safe, So as per Servlet Specification 2.3, " Multiple Servlets executing request threads may hv active access to a single session object at the same time, So the session resources r not thread safe even if servlet implements Single thread Model ! "
Please see to page no: 52 (srv 7.7.1 of chapter 7), of Servlet specification (http://java.sun.com/products/servlet/download.html)
 
Dharmin Desai
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moreover, " Multiple Servlets executing request can simultaneously access and modify the session attributes of a single session object at the same time ! "
( so the bottom line for yr question is : " request object is not part of yr service method so request can not be Synchronized and so session object and even other session resources r not thread safe ! )

Hope i m clear enough !
Best regards, Dharmin
 
reply
    Bookmark Topic Watch Topic
  • New Topic