• Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread problem in Servlet  RSS feed

 
Manish Malhotra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im facing problem of threads while calling different methods of different classes from Servelts.
My scenario is that there could be different users those could access the same servlets simoltaneously & from the service method Im callind different methods of the beans/simple classes which are interactig with database also.
If more then two users are accessing the same screen or other using the same servlet continuosly then its giving abrupt result. I know that every time the servlets called having the same instance but re exicuted the service methods. But I dnt know wat is happening.
And finnaly I made all the methods of my bean as sysnchronized & variables as volatile & finally made the call of the classes from servlet from the Synchronized block itself. But still get errors.
can any body plz help me. Its very urgent.Plz.....
regds,
Manish Malhotra
 
Idly Vada
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem you are getting is probably due to thread synchronization. The servlet container creates a new thread for each servlet request unless the sevlet uses SingleThreadModel.But SingleThreadModel is deprecated and causes performance degradation.
servlet specification says"Multipe servlets executing request threads may hae active access to a single sesin object at the same time. The developer has the responsibility for synchronizing access to session resources as appropriate"
Coming to your problem, put the code which you don't want to be accessed by multiple instances in a synchronized block.You may follow other approaches as well.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As a general rule, thread synchronisation problems in servlets will be reduced if you eliminate or minimise all shared data. Try to avoid member data in your servlet class, using stack-based local variables instead.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!