tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tim Kientzle <>
Subject Re: Servlet unloading
Date Tue, 08 Aug 2000 19:35:35 GMT
Connection pooling isn't really all that complicated,
and there are several freely-available connection pools
floating around on the net.  It's a pretty minor change,

Generally, opening a single connection during init() is
a bad idea, since the servlet engine can process multiple
simultaneous requests.  It does so by invoking your servlet
in multiple threads at the same time.  Having multiple threads
sharing a single DB connection is dangerous.  (Yes, there is
"SingleThreadModel", but that's no panacea, since it causes
the servlet engine to create many copies of your servlet, which
requires more memory and is generally less efficient.)

* A connection pool is highly recommended.  It's scalable
  and offers very good performance with little effort.
  Certainly it's a much smaller change than deploying an
  application server.

* As a short-term solution for lightly-loaded sites, opening
  a new connection at the top of doGet/doPost and closing it
  at the end can be acceptable.  You should carefully time the
  call to "Connection.getConnection()", though.  I've seen
  0.1-0.2 seconds for this call, but I've heard that this can
  take 1-2 seconds.  If it's unacceptably slow, you'll have
  to try something else.  (Just call System.currentTimeMillis()
  before and after and print the difference.)

* What you're doing now is potentially dangerous.  If
  you're still just in early development, you should do
  some stress-testing with multiple clients making
  simultaneous requests; you might well see your servlet crash.

				- Tim

"DAVIS, SCOTT A. (AIT)" wrote:
> Thanks for the suggestion, Jakob.  I've been trying to avoid connection
> pooling since it seems rather complicated to do it correctly and
> efficiently.  If connection pooling ends up a necessity to my project, I may
> just bite the bullet and use a full application server.  I'd hate to have to
> do that at this point, though.
> I modified my database-accessing servlets so that at the beginning of each
> doGet() or doPost() methods they check to see if their database connection
> is valid and open.  If not, they try to create a new connection to the
> database.  I'm hoping that this will resolve the problem for now.
> Scott Davis
> -----Original Message-----
> From: Jakob Hummes []
> Sent: Tuesday, August 08, 2000 10:26 AM
> To:
> Subject: Re: Servlet unloading
> "DAVIS, SCOTT A. (AIT)" wrote:
> > Is it possible to set the amount of time since a servlet is last accessed
> > before Tomcat unloads the servlet from memory?
> This I don't know, but ...
> > I'm working on a intranet application with a number of servlets which each
> > open a connection to a remote database on initialization and close it when
> > they are destroyed.  The remote database is rebooted at 5:30am each Monday
> > morning, breaking any existing connections with the servlets.  Tomcat has
> to
> > restarted for the servlets to reconnect the next time they are accessed.
> should rethink your design.  A better approach is to use a DB
> connection pool.  It should check the status of a connection, and reopen
> the connection if it is broken, before returning the connection to your
> servlet.
> HTH,
> - Jakob

View raw message