• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Concurrency Issue in Servlet

 
kri shan
Ranch Hand
Posts: 1473
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How concurrency issues are solved in Servlet ?
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you will have to tell us a bit more about what you want to know.

The servlet API is by its nature concurrent. The service(..) and doXX(..) methods of a servlet (and thus any methods they call) can and will be executed by many threads at once, under the control of the servlet container.

Are you asking how to ensure that any servlet code you write is "thread safe" (also known as "reentrant") ?
 
Dillip Mohapatra
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kri,

I think, you must localize all your objects and instances that will solve the concurrency problem. For instance, define a separate class, containing all the objects you are using inside the servlet. Now instantiate it inside the servlet and access them through this instance. This should solve your concurrency problem, that normally we face.

Thanks and Regards
Dillip
 
Leena Diwan
Ranch Hand
Posts: 351
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I first time read a solution like that Dilip. Is this widely used in the industry? I doubt this.

Anyone knows?

Regards,
Leena
 
M. Gagnon
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dillip,

What is the advantage of using an inner class defined in a servlet over instantiating some class defined in a separate file and "adding it" by using the setAttribute() method of ServletContext, ServletRequest or HttpSession objects?
 
arnel nicolas
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its up to the programmer to solve it.To avoid this you may want to implement the SingleThreadModel interface in your servlet. There is good article on that here.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leena said:
I first time read a solution like that Dilip. Is this widely used in the industry? I doubt this.

Creating a class to hold user specific data is done all the time. For one thing, it makes things simpler than plugging various bits into the session and hauling them back out again. Note that we are NOT talking about putting everything you use in this class, just the user specific data. If you are using JSP, this class is an obvious candidate for the JavaBean interface.

Another (frequently un-appreciated) reason to create such a user-specific class is the ease of debugging. You can test it outside the servlet environment - a vast simplification.

Bill
(Avoid the SingleThreadModel like the plague - if you think you need it, your architecture is wrong.)
[ July 12, 2004: Message edited by: William Brogden ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic