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?
posted 10 years ago
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.
Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley