tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <>
Subject Re: When does a servlet instance die?
Date Wed, 24 May 2000 16:24:30 GMT
Christoph Mack wrote:

> Hi!
> I understand that if I write a servlet that implements
> the SingleThreadModel concurrent accesses will be
> served by different servlet instances each with its
> own set of instance variables (eg. connections to a
> database).

That is correct, but see below.

> Assuming that there are many many concurrent requests
> occuring this means that there are also many many servlet
> instances running. If each instance opens a database
> connection in its init() procedure there will be a lot of
> connections open.


> My question is: What is tomcat's policy when it comes to
> destroy unused instances (and thereby close the open
> database connections)? Is an unused instance removed
> immediately? What happens if it is the last instance of
> the pool? Is there a timeout value as for sessions?

Servlet containers have the *right* to initialize and destroy servlet
instances any time they want to.  Tomcat's *current* policy is that servlets
are only shut down when you shut down Tomcat itself, or when you have
auto-reloading enabled and Tomcat detects that a class has been changed.

> I have to admit that I did not read the Java Servlet
> Specification word by word but on a quick look I could
> not find anything answering this question. If I am wrong
> please tell me the sections to read...

IMHO, SingleThreadModel is a poor choice for dealing with this kind of issue,
because it requires multiple instances of the servlet class as well as
multiple JDBC connections.  The usual approach to this sort of a problem is a
"connection pool" class that opens a number of connections, and hands them out
to servlets that need them.  You should be able to find references to
available implementations by typing connection pool into a search engine, or
checking the various FAQs.

The connection pool object itself can be shared between servlets and JSP pages
(if you need to), by storing it in the servlet context attributes.

You still end up needing a connection object per simultaneous request -- what
this saves you from is needing a servlet instance per simultaneous request

> thanks in advance
>     Christoph Mack

Craig McClanahan

View raw message