my dog learned polymorphism*
The moose likes Servlets and the fly likes Threads problem in Servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Threads problem in Servlets" Watch "Threads problem in Servlets" New topic
Author

Threads problem in Servlets

Manish Malhotra
Greenhorn

Joined: Apr 05, 2003
Posts: 29
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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12761
    
    5
Sounds to me like you are using instance variables in the servlet for values specific to each user.
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

And instance variables are not thread safe. The only thing thread safe in a Servlet are local variables. See my last night reading helped already.
If you are calling JavaBeans/other classes from your Servlet you need to make them threadsafe the same way as in any Java class would.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Manish Malhotra
Greenhorn

Joined: Apr 05, 2003
Posts: 29
Originally posted by Mark Spritzler:
And instance variables are not thread safe. The only thing thread safe in a Servlet are local variables. See my last night reading helped already.
If you are calling JavaBeans/other classes from your Servlet you need to make them threadsafe the same way as in any Java class would.
Mark

Thx to both of u 4 ur reply. But can u tell me that how to make the JavaBeans/other classes objects threadsafe as u r telling. Can I make all the variables locally defined in the method srevice & then made the service method synchronized.
Right now I hv done the service method & class methods synchronized & make bean variables as volatile.Its seems to be working fine.
But , I think it is not a good practice becz it will overloaded the servlet/classes.
Can anybody of u just give me more description.
Redgs,
Manish Malhotra
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17249
    
    6

Well you shouldn't synchronize the service method. If you want the service method synchronized just implement the SingleThreadModel interface.
Now to make the JavaBeans/other classes to be thread safe you need to use the Synchronized keyword on those methods in those classes.
Mark
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

If you want the service method synchronized just implement the SingleThreadModel interface.


SingleThreadModel will be deprectaed in Servlet 2.4
http://www.onjava.com/lpt/a/2825
[ September 29, 2003: Message edited by: Pradeep Bhat ]

Groovy
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads problem in Servlets
 
Similar Threads
Servlet Container related query
Thread problem in Servlet
Servlets, Sessions & Thread Safety??
strange problem
some java interview Questions