aspose file tools
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
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Servlets
Reply 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: 204

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) - Mock Tests
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: 204

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: 204

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://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Do I need to synchronize session in this case?
 
Similar Threads
parameters, headers, and attributes
Session handling in servlets
confused with ajax and http request
setAttribute() and getAttribute()!!!
passing object from one servlet to the other.