wood burning stoves*
The moose likes Servlets and the fly likes Threading issues in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "Threading issues in servlet" Watch "Threading issues in servlet" New topic
Author

Threading issues in servlet

Subbu Aswathanarayan
Ranch Hand

Joined: Jun 22, 2001
Posts: 73
Hi all,
I am writing an application which involves a servlet which processes user request.This servlet might be accessed by more than one user simultaneously.My question is will the servlet engine(i am using JRun) or the webserver (IIS) take care concurrent user request?is there anything i need to do to make sure that my application doesn't crash?is there any precaution i should take?
also can i call the destroy() method at the end of the servlet?
any help would be greatly appreciated.
Subbu


Subbu
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
I think you will not have to worry about the concurrent issue in this case unless you use SingleThreadModel for your servlet.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12792
    
    5
It is the responsibility of the servlet container to assign a separate Thread to each request. It is the responsibility of the programmer to keep in mind that multiple Threads will be executing the same servlet object. Don't use instance variables for user-specific data, etc.
NEVER call destroy() that is what the servlet container does before removing an instance of your servlet.
Bill

------------------
author of:
Subbu Aswathanarayan
Ranch Hand

Joined: Jun 22, 2001
Posts: 73
Hi SoonAnn,
what is the singlethreadmodel and where do we use it?please reply asap.
Subbu
Subbu Aswathanarayan
Ranch Hand

Joined: Jun 22, 2001
Posts: 73
Originally posted by William Brogden:
It is the responsibility of the servlet container to assign a separate Thread to each request. It is the responsibility of the programmer to keep in mind that multiple Threads will be executing the same servlet object. Don't use instance variables for user-specific data, etc.
NEVER call destroy() that is what the servlet container does before removing an instance of your servlet.
Bill


Hi Bill,
Thanks for the reply.I will be more specific about my problem.My servlet is going to access a database and will read and write to the database.My question is when one servlet thread is writing to the database how can another servlet read from or write to the same database.
please explain how should i handle this situation.
Thanks.
Subbu
SoonAnn Lim
Ranch Hand

Joined: Jun 21, 2001
Posts: 155
Hi, Subu,
I get the point backward. You may use SingleThreadModel interface to avoid concurrency issue. When using SingleThreadModel interface, you do not have synchronization issue because server will guarantee no 2 threads will execute the service method of the servlet. Be awared this approach is not very efficient for large hit web pages.
 
wood burning stoves
 
subject: Threading issues in servlet