Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ServletContext & synchronization?

 
hasan khan
Ranch Hand
Posts: 223
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • 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?
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • 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.
 
Arvind Giri
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic