It's not a secret anymore!
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Synchronizing scoped attributes. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Synchronizing scoped attributes. " Watch "Synchronizing scoped attributes. " New topic

Synchronizing scoped attributes.

Steven Colley
Ranch Hand

Joined: Feb 18, 2005
Posts: 290

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?


SCJP | SCWCD | SCBCD | SCWSD 5 | SCEA (I) 1.4 | SCEA 5 | IBM SOA 669
Charles Lyons
Ranch Hand

Joined: Mar 27, 2003
Posts: 836
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.

Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Steven Colley
Ranch Hand

Joined: Feb 18, 2005
Posts: 290
hum..that�s right!

Tks Charles!
I agree. Here's the link:
subject: Synchronizing scoped attributes.
jQuery in Action, 3rd edition