• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
sathiya moorthy
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
Posts: 232
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic