This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

service() method

 
Kedar Dravid
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The service() method is not threadsafe. Only one instance of a servlet is created. For each request, a new thread is spawned.
My question:
If some code is written in the service class which updates a database table, and different clients access the servlet at the same time (in effect, the service() method) then how is database consistency handled?
 
Bosun Bello
Ranch Hand
Posts: 1510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am assuming you mean service method when you said "If some code is written in the service class ".

First, you are not supposed to override the service method. This mathod is called by the container based on the requests it receives from the client. Your code should go in doGet/doPost and the service method will call the appropriate one.

Secondly, if you have any concern regarding multiple requests accessing/updating your database simultanously, the you have to handle that in your code in doPost/doGet. You can synchronize the piece of logic that updates the database so that only one thread of your particular servlet instance will be able to access it at a time.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic