You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Pavan Singaraju <ma...@gmail.com> on 2007/09/27 12:39:50 UTC

Simultaneous Requests to servlet

Hi,
    I have a basic question about servlets in Tomcat 5.0.18. In theory, when
there are simultaneous requests come for a servlet, it will either queue the
requests and serve one by one or create multiple threads of the servlet and
serve the request.
    My question is, what / how tomcat handles this situation?
    I need the answer for this, because, i have a synchronized access to an
object from the servlet, and if tomcat handles the monitor for the object
then i don't need to handle the monitoring condition else i may have to do
it.
    I understand that Tomcat has a SingleThreadModel interface that allows
only one thread access/One request at a time model. But help me solve the
previous problem
--
Pavan S. Kumar

Re: Simultaneous Requests to servlet

Posted by David Smith <dn...@cornell.edu>.
Christopher Schultz wrote:

>-----BEGIN PGP SIGNED MESSAGE-----
>Hash: SHA1
>
>David,
>
>David Smith wrote:
>  
>
>>As I understand it one instance of the servlet per webapp is accessed
>>from all threads in a reentrant manner.  This is why you normally can't
>>use any class instance variables in a servlet.
>>    
>>
>
>While this is the most straightforward implementation of a servlet
>container, the servlet specification makes no guarantees whatsoever
>about the lifecycle of a specific servlet. The container is free to
>create a new instance of the servlet to handle every request, or to keep
>a single copy around for the life of the webapp. Or anything in between.
>  
>

Yup.... and all the more reason to store this synchronized data object 
in the servlet context, implementing logic to make it thread-safe.

>The only guarantee made along these lines is for servlets that implement
>SingleThreadModel, which will either require serialized access or
>multiple instances to handle requests. Since this has been deprecated
>but not removed from the spec, I'll bet it will continue to be supported
>  
>
>forever, but it's not a very good idea IMO.
>
>- -chris
>-----BEGIN PGP SIGNATURE-----
>Version: GnuPG v1.4.7 (MingW32)
>Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
>
>iD8DBQFG+64J9CaO5/Lv0PARAjpoAJ0WrN71Y3DsZMwsH3Id7yyVw7eSNgCgwenS
>7F7lp0R5RGdDyYeopo6oQO4=
>=Ki+D
>-----END PGP SIGNATURE-----
>
>---------------------------------------------------------------------
>To start a new topic, e-mail: users@tomcat.apache.org
>To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
>For additional commands, e-mail: users-help@tomcat.apache.org
>
>  
>


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Simultaneous Requests to servlet

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

David,

David Smith wrote:
> As I understand it one instance of the servlet per webapp is accessed
> from all threads in a reentrant manner.  This is why you normally can't
> use any class instance variables in a servlet.

While this is the most straightforward implementation of a servlet
container, the servlet specification makes no guarantees whatsoever
about the lifecycle of a specific servlet. The container is free to
create a new instance of the servlet to handle every request, or to keep
a single copy around for the life of the webapp. Or anything in between.

The only guarantee made along these lines is for servlets that implement
SingleThreadModel, which will either require serialized access or
multiple instances to handle requests. Since this has been deprecated
but not removed from the spec, I'll bet it will continue to be supported
forever, but it's not a very good idea IMO.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFG+64J9CaO5/Lv0PARAjpoAJ0WrN71Y3DsZMwsH3Id7yyVw7eSNgCgwenS
7F7lp0R5RGdDyYeopo6oQO4=
=Ki+D
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Simultaneous Requests to servlet

Posted by David Smith <dn...@cornell.edu>.
Pavan Singaraju wrote:

>Hi,
>    I have a basic question about servlets in Tomcat 5.0.18. In theory, when
>there are simultaneous requests come for a servlet, it will either queue the
>requests and serve one by one or create multiple threads of the servlet and
>serve the request.
>    My question is, what / how tomcat handles this situation?
>  
>

As I understand it one instance of the servlet per webapp is accessed 
from all threads in a reentrant manner.  This is why you normally can't 
use any class instance variables in a servlet.  The only queueing that 
occurs is in the connector's thread pool when all threads are busy 
handling requests.

>    I need the answer for this, because, i have a synchronized access to an
>object from the servlet, and if tomcat handles the monitor for the object
>then i don't need to handle the monitoring condition else i may have to do
>it.
>    I understand that Tomcat has a SingleThreadModel interface that allows
>only one thread access/One request at a time model. But help me solve the
>previous problem
>  
>

SingleThreadModel has it's uses, but is also hard to get right and 
usually discouraged because of that.  I'd say you are going to have to 
implement monitoring to prevent concurrent access where necessary.  Just 
try to keep the sync blocks small so a minimum amount of time is spent 
there and only as necessary.

Your object is probably best served stored in the servlet context (ie 
application scope) with logic that handles synchronizing on update 
operations.


--David

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


RE: Simultaneous Requests to servlet

Posted by Peter Crowther <Pe...@melandra.com>.
> From: Pavan Singaraju [mailto:mailsforapache@gmail.com] 
> In theory, when
> there are simultaneous requests come for a servlet, it will 
> either queue the
> requests and serve one by one or create multiple threads of 
> the servlet and serve the request.
> My question is, what / how tomcat handles this situation?

It has already created and will use multiple threads - possibly hundreds
in a large system.  You need to handle synchronisation yourself, and for
a high-throughput site you need to handle it very carefully so that you
don't artificially limit how much can be run in parallel.

		- Peter

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Simultaneous Requests to servlet

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Pavan,

Pavan Singaraju wrote:
>     I have a basic question about servlets in Tomcat 5.0.18. In theory, when
> there are simultaneous requests come for a servlet, it will either queue the
> requests and serve one by one or create multiple threads of the servlet and
> serve the request.

Nope: all requests will be handled in parallel unless other factors
exist (such as more requests than your server is configured to accept
simultaneously).

There used to be a SingleThreadModel interface you could implement that
would force the behavior you describe but it has been deprecated.

>     My question is, what / how tomcat handles this situation?

Tomcat does not take any special action when multiple requests come in
for a particular servlet.

> I need the answer for this, because, i have a synchronized access to an
> object from the servlet, and if tomcat handles the monitor for the object
> then i don't need to handle the monitoring condition else i may have to do
> it.

Tomcat will never handle monitors for your synchronized objects: the JVM
will do it. If you need serialized access to an object, then you need to
synchronize it yourself. Better yet, make multiple copies of it so you
don't have to serialize access to it (them). Even better, fix your
design so that it's thread safe and you don't need any synchronization
at all.

> I understand that Tomcat has a SingleThreadModel interface that allows
> only one thread access/One request at a time model.

SingleThreadModel has been deprecated as of Servlet API v2.4. Don't use it.

> But help me solve the previous problem.

You can alway do this:

private Whatever _myProtectedResource;

public void service(HttpServletRequest req, HttpServletResponse rsp)
{
    ...
    synchronized (_myProtectedResource) {
        // do whatever you need to do in here that requires exclusive
        // access to _myProtectedResource
    }
    ...
}

Hope that helps,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFG+6ya9CaO5/Lv0PARAgTKAKC4UFTpSjZm65Kuv9390onquuZQWACgt2Sw
L6jIt3B3woKIxvM/jVSPs+4=
=4Ge9
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Simultaneous Requests to servlet

Posted by David Delbecq <de...@oma.be>.
Pavan Singaraju a écrit :
>     I understand that Tomcat has a SingleThreadModel interface that allows
> only one thread access/One request at a time model. But help me solve the
> previous problem
> --
> Pavan S. Kumar
>   
Don't use SingleThreadModel anyway. From J2EE 1.4 docs:

Deprecated. As of Java Servlet API 2.4, with no direct replacement.

public interface SingleThreadModel

Ensures that servlets handle only one request at a time. This interface 
has no methods.

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.

Note that SingleThreadModel does not solve all thread safety issues. For 
example, session attributes and static variables can still be accessed 
by multiple requests on multiple threads at the same time, even when 
SingleThreadModel servlets are used. It is recommended that a developer 
take other means to resolve those issues instead of implementing this 
interface, such as avoiding the usage of an instance variable or 
synchronizing the block of the code accessing those resources. This 
interface is deprecated in Servlet API version 2.4.



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org