This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
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


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Synchronizing scoped attributes. " Watch "Synchronizing scoped attributes. " New topic
Author

Synchronizing scoped attributes.

Steven Colley
Ranch Hand

Joined: Feb 18, 2005
Posts: 290
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


SCJP | SCWCD | SCBCD | SCWSD 5 | SCEA (I) 1.4 | SCEA 5 | IBM SOA 669
Charles Lyons
Author
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: http://aspose.com/file-tools
 
subject: Synchronizing scoped attributes.
 
Similar Threads
Sync block and variable references
Doget, Dopost
passing data
Collections synchronization
Collection synchronization question