• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Do I need to synchronize session in this case?

 
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no need to synchronize you session methods
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic