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

Doget, Dopost

 
Rahul Bajaj
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are doget and dopost methods in servlets thread safe? Should we synchonise them to make thread safe.
 
Rizwan Mohammad
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
doGet and doPost methods are not syncronized. When these methods are operated on instance variables or static variable they are not protected. In servlets only local variables and request scope variables are threads safe. Remaining all(session scoped variables, context scoped variables, static variables) are not thread safe. Instance variables are thread safe only when servlet implements single thread model interface, which is deprecated in servlet 2.4 spec..
HTH.
 
Bryan Basham
author
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer is "no", the doGet and doPost (any doXyz) methods are not synchronized and thus not thread-safe. However, it is a very bad practice to synchronize these methods for your own servlet classes. The reason is that it reduces the servlet's ability to scale; IOW, to handle large numbers of concurrent requests.

This topic (making a webapp thread-safe) is rather complex and one that cannot be resolved in a single answer. So, what I would recommend is that you study fundamental aspects of concurrent programming. I would recommend Doug Lea's book Concurrent Programming in Java. It is a rich study of this broad topic. However, I will warn you that it is also very dense reading.... not exactly a Head-First experience.

The basic advice I can give you is this: Synchronize the smallest block of code that accesses or modifies a shared memory state. Remember: synchronizing reduces the scalability and at times the responsiveness of your webapp (any application, really) so try to limit or even remove the need for synchronization.

Clearly the doGet/doPost methods are in-fact the largest possible block of code to synchronize so that should be a clear "no no".

Another important element of my advice is "shared memory state". Consider this carefully. There are six fundamental variable scopes in a webapp. Which of these scopes are truly thread-safe?

  • Application (servlet context) scope attributes
  • Session scope attributes
  • Request scope attributes
  • Local method variable (or JSP page scope attributes)
  • Servlet instance variable
  • Servlet class variable


  • In general, only the local/method variables and Request scope attributes are thread-safe. Whenever, you access or modify a variable/attribute in any of the other scopes you should synchronize.

    This is just the beginning of a very rich and interesting topic.

    Good luck,
    Bryan
    [ November 28, 2005: Message edited by: Bryan Basham ]
     
    Rahul Bajaj
    Ranch Hand
    Posts: 45
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Rizwan , Bryan. I got the idea now.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic