File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes service() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "service() method" Watch "service() method" New topic

service() method

Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
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

Joined: Nov 06, 2000
Posts: 1510
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.

So much trouble in the world -- Bob Marley
I agree. Here's the link:
subject: service() method
It's not a secret anymore!