Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do I need to synchronize session in this case?

 
Varun Chopra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Servlets methods should be idempotent so synchronisation shouldn't be required. Can you post your code? And maybe the stack trace?
 
Varun Chopra
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is no need to synchronize you session methods
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic