• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Synchronizing scoped attributes.

 
Steven Colley
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

a question regarding synchronizing the access to the list of users with whatever scope.

How should i go about?

1 - should i sync the scope itself? (session, context..)
synchronized(context) or synchronized(session)...

2- or should i sync the list object itself instead?
synchronized( getServletContext().getAttribute("userList") )
synchronized( req.getSession().getAttribute("userList") )

What is the best approach?


Tks
 
Charles Lyons
Author
Ranch Hand
Posts: 836
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you synchronise the list object itself, you won't be able to stop it being removed as an attribute from the scope... Someone could still invoke, for example, session.removeAttribute("userList") or session.setAttribute("userList") to remove or replace it, respectively, and there's nothing you can do. Synchronising on the session/context object itself prevents removal/replacement and also prevents other objects from obtaining a reference to the scoped variable using getAttribute() - the only thing it doesn't do is prevent modification to the list contents if they already have a handle on the list (which is unlikely if they couldn't call getAttribute()).

Hope that helps.
 
Steven Colley
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hum..that�s right!

Tks Charles!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic