Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Session Interference

 
Raghav Sam
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I'm getting weird things on my user sessions, so i was wondering... ...Is the HttpSession thread-safe? I'm having users objects 'setted' on other users sessions, so, may it be that i need to enclose the code in a 'synchronized' block? I'll appreciate any help.
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, HttpSession should be threadsafe. You don't need to synchronize access to it.
Are you saying you're getting objects from one user's session placed on another user's session?
Kyle
 
Raghav Sam
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kyle Brown:
Yes, HttpSession should be threadsafe. You don't need to synchronize access to it.
Are you saying you're getting objects from one user's session placed on another user's session?
Kyle

Yes. There seems to be a mixup. I am actually storing value objects (obtained from the user input) in session and using the info in subsequent pages.
On one occasion, the entire contents of the bean seems to have been overwritten with some other data.
If I should not synchronize it, how should I make sure that data integrity is maintained between different sessions.
Raghav
 
shankar vembu
Ranch Hand
Posts: 309
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Raghav Sam:

Yes. There seems to be a mixup
On one occasion, the entire contents of the bean seems to have been overwritten with some other data.

This will not happen. I guess you should check your application logic...
Originally posted by Raghav Sam:

If I should not synchronize it, how should I make sure that data integrity is maintained between different sessions.

You dont have to worry about maintaining data integrity b/w different sessions...
Regards
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On one occasion, the entire contents of the bean seems to have been overwritten with some other data.
If I should not synchronize it, how should I make sure that data integrity is maintained between different sessions.

I am betting that you are storing a reference to the bean as an instance variable in your servlet class. Remember - any number of requests can be undergoing processing "at the same time" so you can't use instance variables to store anything that changes with the user.
Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic