tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Randy Layman <>
Subject RE: Database servlets
Date Mon, 11 Feb 2002 13:14:06 GMT

> -----Original Message-----
> From: John Wadkin []
> Sent: Monday, February 11, 2002 8:47 AM
> To: Tomcat Users List (E-mail)
> Subject: Database servlets
> All,
> Although I don't need to write servlets, I thought it might 
> be useful to
> gain an understanding of how they work. Wished I hadn't bothered :)
> A test servlet that connects to a database, read/writes, and 
> closes the
> connection, fails when more than one person requests the 
> servlet. The author
> of the servlet tells me that the servlet isn't multi-user and 
> needs a few
> "synchronised" statements.
> I thought that:
> A servlet creates a new instance of itself for each request
> Oracle can handle multiple users
> In view of this, I don't see why the servlet doesn't work or 
> why it needs
> "synchronising". Is there only one instance of the servlet 
> for all requests
> - i.e. the object that represents the database connection is 
> shared, hence
> the need for synchronisation. If so, then this means that only one
> request/client can use the database at any given time - 
> effectively making
> Oracle a single user database. This can't be right. What am I not
> understanding? I've gone through Sun's servlet tutorial and 
> I'm none the
> wiser!

Generally speaking, all requests are handled by one servlet instance (there
are a few finer points to this statement that I will clarify in a moment).
It does NOT mean that only one connection to the database can be created at
a time.  It means that the design that you used will only allow one request
at a time.  I'm guessing that your design is something like:
	init() - create connection
	doGet/doPost - process database stuff
	destroy() - close connection

The problem with this, as you have seen, is with concurrency and multiple
uses.  A better choice would be to move all connection handling into the
doGet/doPost.  An even better choice would be use a component that supports
connection pooling - the component opens up a number of database connections
and hands them out as needed by the application.

Not for the finer point that I mentioned earlier.  There are two ways that I
know of to get multiple sevlet instances:
	1.  Create different servlet definitions in the web.xml file for
different URLs.  This allows servlets with different URLs to have different
startup parameters and state
	2.  Make your servlet implement SingleThreadModel and then use a
container that supports pooling of the servlets.  The SingleThreadModel
guarantees that only one request will use a servlet object at a time.  The
Servlet spec allows the container to pool these servlets if it desires to
add enhanced performance.  This is pooling of servlets is not a requirement
and is implemented in Tomcat 4, but not Tomcat 3.


To unsubscribe:   <>
For additional commands: <>
Troubles with the list: <>

View raw message