This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Servlets and the fly likes Do not synchronize doGet, doPost? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Do not synchronize doGet, doPost?" Watch "Do not synchronize doGet, doPost?" New topic
Author

Do not synchronize doGet, doPost?

Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
While searching for some material on servlets, I came upon this
page. Do you think this is correct?

thanks,
Alex


All right brain, you don't like me and I don't like you, but let's just do this one thing so I can get back to killing you with beer.<br /> <br />- Homer Simpson
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61106
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
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

Joined: Jan 10, 2002
Posts: 61106
    
  66

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

Joined: Jan 24, 2001
Posts: 476
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

Joined: Jan 24, 2001
Posts: 476
sorry for double reply-post. thanks a lot.
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
sorry for double reply-post. thanks a lot.
Alex Kravets
Ranch Hand

Joined: Jan 24, 2001
Posts: 476
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.
 
 
subject: Do not synchronize doGet, doPost?