This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes Do I need to synchronize session in this case? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Do I need to synchronize session in this case?" Watch "Do I need to synchronize session in this case?" New topic
Author

Do I need to synchronize session in this case?

Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
On my web app, I have a page with 3 iFrames. Each iFrame does a similar request to server to load a page but they send their own unique pageId to server. For e.g. frame 1 request sends pageId=100, frame2 sends pageId=101 and frame3 sends pageId=102. Same servlet method is called on server side for each frame.

That serverside method (say sharedMethod()) calls setAttribute/getAttribute methods on session object BUT most important point is setAttribute/getAttribute append pageId to attribute names to make them unique per page. That means if I have a session attribute TITLE, shareMethod() sets/gets attribute name TITLE100, TITLE101 AND TITLE102 for each page, same attributes names are not used.

Sometimes I see nullpointer exceptions, which I am unable to trace so far - it happens rarely. One doubt I have is do I need to synchronize access to sharedMethod or at least to code that manipulates session object? If yes, can you explain to me why is the need?


-Varun -
(My Blog) - Online Certifications - Webner Solutions
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Servlets methods should be idempotent so synchronisation shouldn't be required. Can you post your code? And maybe the stack trace?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
Servlets methods should be idempotent so synchronisation shouldn't be required. Can you post your code? And maybe the stack trace?


simplified code is this:


I do not necessarily get the exception on session statements. It is intermittent and random.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

It's hard to offer advice if that is not the actual code, but the only NullPointerException that can occur there is if pageId is not in the request. And you don't need to synchronise access to your shared method.
Varun Chopra
Ranch Hand

Joined: Jul 10, 2008
Posts: 211
It's hard to offer advice if that is not the actual code, but the only NullPointerException that can occur there is if pageId is not in the request. And you don't need to synchronise access to your shared method.


That's fine Paul. But my main concern is if we need to synchronize session methods or not (especially when same user/session can spawn multiple servlet threads by sending simultaneous requests)......This can be considered as my main question.
Henry Martin
Greenhorn

Joined: Aug 20, 2009
Posts: 5
There is no need to synchronize you session methods
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Do I need to synchronize session in this case?
 
Similar Threads
confused with ajax and http request
passing object from one servlet to the other.
parameters, headers, and attributes
Session handling in servlets
setAttribute() and getAttribute()!!!