Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Do not synchronize doGet, doPost?

 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
While searching for some material on servlets, I came upon this
page. Do you think this is correct?

thanks,
Alex
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Absolutely. If you synchronize the method, then you serialize access to it. Now imagine what happens when 100 people hit the servlet at the same time.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Bear.
The reason I am asking is because a colegue of mine at work says that a server that supposed to return a unique number from a database sometimes returns the same number if server is hit twise. He says that making doGet() of the servlet synchronized will solve the problem. I say we need to make a synchronized block in the method and put code that accesses same resource in there.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64633
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your instinct is correct. The "lower down" you synchronize, the better as the critical section should be as tight/short as possible.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Bear.
The reason I am asking is because a colegue of mine at work says that a server that supposed to return a unique number from a database sometimes returns the same number if server is hit twise. He says that making doGet() of the servlet synchronized will solve the problem. I say we need to make a synchronized block in the method and put code that accesses same resource in there.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for double reply-post. thanks a lot.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for double reply-post. thanks a lot.
 
Alex Kravets
Ranch Hand
Posts: 476
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for double reply-post, for some reason I typed the reply and it did not show up in the thread. Had to post again. thanks a lot.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic