• 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

ServletContext & synchronization?

 
Ranch Hand
Posts: 223
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All my servlet in my web application is using synchronized block on ServletContext.

So the problem is if 1 servlet acquire the lock on ServletContext object, then the rest has to wait. And i don’t want my servlets to have dependencies on each other.

So I am using the class level synchronize blocks instead of synchronizing ServletContext object.

Please tell me is it the right solution to my problem. Will the class level synchronization will block other request to the same servlets.

will it create some other problems or blocking?
 
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you are using synchronized block on ServletContext then other threads or servlets has to wait for lock there is no doubt about it.
now you are talking about synchronized block on class level.
if you are synchronized on service method then your service variables are
synchronized but your ServletContext or Session Attributes are not synchronized .
To synchronized ServletContext level attributes you dont not have any choice accept to synchronized on ServletContext object.same is true for Session attributes.
If you dont want to synchronized on ServletContext object at all then dont use ServletContext attributes use request attributes or local variables only.
Because request attributes and local variables are bydefault thread safe.
if you will provide your source code it will be easy for me to explain thread issues.
 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So I am using the class level synchronize blocks instead of synchronizing ServletContext object.



Which lock you are using for synchronization? Are you using 'this' lock?

If you are using this lock then please mind that using 'this' lock in your servlet/jsp for synchronizing context attribute will not save you. As different servlet/jsp can still access the context attributes in unsynchronized manner.
 
hasan khan
Ranch Hand
Posts: 223
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator




Like this i have 5 different servlets.

my code is simple, each servlet makes remote call whenever it gets first request and store the map in to the context attribute.
This approach works fine to make sure that each remote call is made once and only once in the life time of the application.

but i dont want any of them to block for each other.
since all 5 servlets are different servlets making 5 different remote call they should execute independently.
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it should work fine.

Although if other request comes when map is being retrived from remote, then they will have to wait until map is retrieved. while all other request will not block.

Please correct me if I am wrong?
 
The happiness of your life depends upon the quality of your thoughts -Marcus Aurelius ... think about this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic