On Tue, 27 Jul 1999, Nitin Kulkarni wrote:

> Hello Friends,
>
> I need some clarification.
>
> I tried the following thing
>
> I wrote a servlet which extended HttpServlet and implemented
> SingleThreadModel
>
> In the init method of this servlet I put a display statement just to
> find out how many times it gets called i.e how many instances of
> this servlet would get created.
>
> Then I invoked this servlet thru an HTML page. The display statement
> was shown 6 times which means that the server created 6
> diff. instances of the same servlet. Am I right? How was this figure
> of 6 reached at?

Yes, you are right.  Exactly how SingleThreadModel is handled, and
exactly how many instances are created is dependent on the servlet
engine (although I think most/all do create a pool of instances).
Some servlet egnines may even allow you to control how many instances
are created, initiallly, and at maximum.  What servlet engine are you
using?


> How is this pool of instances used by the server? I then invoked the
> servlet more than 6 times and then expected the server to create
> some more instances to satisfy the additional requests however this
> did not happen. The server seamed to reuse the existing pool because
> it seems that the servlet after having sent back the response
> becomes free to handle new requests.Is this right?. The server then
> searches thru the existing pool to find out if any servlet instance
> is free to handle to new request or not.

I believe you are right again.  Remember, the invocations have to be
concurrent for there to be a conflict, and hence the need to create
more instances.  A true test of the behavior would be to have more
than six concurrent invocations.


> Also in this scenario the database connection got established 6
> times.  Is there any way to establish the database connection only
> once but still use SingleThreadModel

Well, you could set up a connection pool, managed as a Singleton
instance (kind of like the SingleThreadModel servlet is managed by the
servlet engine).  Actually, you might want to do this regardless of
whether you use SingleThreadModel.  And regarding SingleThreadModel,
what many recommend is to not use it at all, instead designing your
code more cleanly so it handles multi-threading, with minimal,
isolated synchronization if necessary.  SingleThreadModel is in some
way overkill, and furthermore doesn't always solve the problem.

(I'll refer you to the list archives for discussions of database
connection pools and the pros and cons of SingleThreadModel.)

Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]

___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".

Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html

Reply via email to