aspose file tools*
The moose likes Servlets and the fly likes what's the use of synchronizing the service in servlet? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "what Watch "what New topic
Author

what's the use of synchronizing the service in servlet?

sathiya moorthy
Greenhorn

Joined: Nov 27, 2004
Posts: 9
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?

thanks in advance
sathiyan.v
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

"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.


Dave
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
As per Server 2.4 java docs,

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.

but,look at this piece of code.

public void doPost(HttpServletRequest req, HttpServletResponse res)
{
public synchronized void getDetails()
{
//do something here
}
}

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.
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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.

Dave
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: what's the use of synchronizing the service in servlet?