tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From John Wadkin <j.wad...@hud.ac.uk>
Subject RE: Database servlets
Date Mon, 11 Feb 2002 14:34:24 GMT
Randy,

Thanks for the reply. Interesting stuff! You guessed that the servlet
follows this structure:

init() - create connection
	doGet/doPost - process database stuff
	destroy() - close connection

Well that's how I thought it -should- look but it doesn't! The connection
open/close is in the doGet/Post method. Sun's servlet tutorial implies that
the "open in init" method is preferable but that didn't make sense to me
'cos of the connections thing.

You said:

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.

So that's what database pooling is :) Must be pretty sophisticated stuff -
managing connections so that there's an "appropriate" number available. I
see references to it on the list and always wondered :)

Re. SingleThreadModel and pooling servlets - I knew about STM but didn't
know that pooling servlets was possible or that TomCat supported it. There
doesn't appear to be any mention in the documentation.

So, 'cos the open/close connection is in the doGet/Post, there shouldn't be
a problem with concurrent users? That's worrying :) I tested concurrency
using a colleague - I counted down from three and we clicked the "Refresh"
button in our browsers. Sophisticated, eh? On my machine the servlet worked
fine, but on my colleague's machine the servlet stopped when dropping the
table. No errors anywhere. I'm guessing that this is because my request
removed the table first or/and closed the connection. If so, then the issue
becomes one of "how to stop that happening" or "how to handle the fact that
this has happened". It makes sense that one request might "pull the rug from
under" another request - i.e. alter a shared table. I guess that the SQL in
the servlet is "unusual" in that it isn't normal practice to remove tables
(e.g. delete the "customer" table)! I think it's fair to say that it's good
practice to check that something exists first before trying to do something
with it (!) - i.e. I'd check that the table existed before trying to delete
it.

The servlet (attached) just tests connectivity in preparation for the
installation of a larger servlet, but as I said I'm interested in the hows
and whys!

Thanks,
 
John
 

-----Original Message-----
From: Randy Layman [mailto:randy.layman@aswethink.com]
Sent: 11 February 2002 13:14
To: 'Tomcat Users List'
Subject: RE: Database servlets

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.

	Randy


Mime
View raw message