what's the use of synchronizing the service in servlet?
sathiya moorthy
Greenhorn
Joined: Nov 27, 2004
Posts: 9
posted
0
hello can any one please explain me the following doubts 1) what's the use of synchronizing the servlet's service method? 2) what's the difference between single thread model and synchronzied service method?
"sathiyan.v" Please see Jim's request for you to change your doisplay name here.
We require display names to be two words: your first name, a space, then your last name. Please try again
----
1) what's the use of synchronizing the servlet's service method? I'm not sure if we should justify this with an answer, it's such a bad idea Synchronising the service method ensures single threaded access to a Servlet Class. This will probably result in severe performance problems.
2) what's the difference between single thread model and synchronzied service method? From the API:
The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.
SingleThreadModel interface is deprecated in Servlet 2.4. If a servlet implements this interface, you are guaranteed that no two threads will execute concurrently in the servlet's service method. The servlet container can make this guarantee by synchronizing access to a single instance of the servlet, or by maintaining a pool of servlet instances and dispatching each new request to a free servlet.
as I see it, using synchronized for service method should give similar results as using the SingleThreadModel interface.
In the above method, the first request from the client gets the lock to the thread accessing the getDetails method. Another request will have to wait in the service method until the lock is released. Using synchronized for a method inside a service may allow multiple threads to access the same service and wait until the synchronized method(getDetails in above case) is complete by thread using it.
Whereas in SingleThreadModel, "you are guaranteed that no two threads will execute concurrently in the servlet's service method".
But I have never any application until now that has implemented the SingleThreadModel or using synchronized( ) for service in a servlet.
as I see it, using synchronized for service method should give similar results as using the SingleThreadModel interface.
... except when the container implements the SingleThreadModel behaviour using a pool of instances.
If you synchronize the service method, all instances will still be synchronized, even the ones in the servlet pool. ie synchronization always makes it behave like a single synchronized instance and loses the benefit of the SErvlet Pool.