• 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

Synchronizing scoped attributes.

 
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Author
Posts: 836
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hum..that�s right!

Tks Charles!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic