• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

service() method

 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic