• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threading issues in servlet

 
Subbu Aswathanarayan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
SoonAnn Lim
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi SoonAnn,
what is the singlethreadmodel and where do we use it?please reply asap.
Subbu
 
Subbu Aswathanarayan
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic