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

Context Attributes are not thread safe??

 
Vinay Kumar chowdary
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I came across these while reading HFSJ:

The best way is to synchronize on the ServletContext object.
synchronized (getServletContext())
This make the context thread-safe ONLY if ALL the servelts synchronize on the context.


However, I am bit confused abt the last line "This make the context thread-safe ONLY if ALL the servelts synchronize on the context"

can anyone explain this?

Thanks,
 
Gowher Naik
Ranch Hand
Posts: 643
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Context Attributes are not thread safe.
You can make Context Attributes thread safe if you synchronize on ServletContext object.
Context Attributes will be thread safe if all servlets accessing context attributes are synchronized.
Check yourself by synchronize context object in one servlet and in another servlet dont synchronize context object and see result.
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that if you forget to do this in at least ONE of the servlets, then it's not thread safe anymore. It has to be done in ALL servlets in the application.
 
Vinay Kumar chowdary
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Gowher and Satau,

Thanks for that..I understood now...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic