• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

synchronized(session) will really help...?????

 
kajal mukergi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends...

in page no. 200 HFS it is said that.. synchronized(session);
will help.. us for making consistent

but small doubt... is here we have done this session synchronize

but problem is that..
when the user opens a new browser and.. he ask for, then this will consider in the same session...

so if the user opens.. 3 web broser pages and.. try to manipulate the values..

no one can stop him despite of.. make "session synchronize"

if i am wrong in this concept please help me to rectify.. and explain me...as i am new...

Thank you very much
bye,.......
 
Mark Garland
Ranch Hand
Posts: 226
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The problem is that different requests (from different browsers on the same machine, or different machines) are handled by the container as
different THREADS
acting upon a single INSTANCE of the servlet.

So, these different threads could concurrently access the session, and you could get results that you weren't expecting.

Synchronizing on the session means that whenever a thread wants to access the session, it must first access the session object's lock. There is only a single lock, so only one thread can access the session at once. This protects the session (and you) from getting results you weren't expecting from threads concurrently accessing the session.

The downside of course, is that while one thread has the lock, all other threads are blocked, so synchronizing for long periods of time could be bad for performance. I think HFSJ gives the advice to "get in, do whatever you need and get out".

Hope that helps,
 
kajal mukergi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mark

Thank you very much now i got the complete idea on it..

thanks
bye..
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic