durusmail: quixote-users: Database connections: opening, closing, error handling
Database connections: opening, closing, error handling
2002-07-13
2002-07-15
Database connections: opening, closing, error handling
Joel Shprentz
2002-07-13
Within a Quixote application running with FastCGI, what is the best
strategy for opening and closing database connections and for handling
database errors?

Before sending queries to a database with Python Database API v2.0 (and
most other database APIs), a connection must be established.  Where in a
Quixote application should the connection be opened?  In the driver
script?  In a subclass of Publisher?  In a function or method that handles
a request?

Should the connection ever be closed explicitly?  It will be closed
automatically when Apache and FastCGI terminate the application.

Database APIs can raise many types of exceptions.  Queries can fail,
connections can be lost, and disks can fill.  Where is the best place to
convert a database exception into a subclass of Quixote's publisher error?
(I have my eye on Publisher.finish_failed_request).

Database errors can occur very early (when establishing a connection).  In
the regrettably common case where the database server is down and the web
server is up, we want to provide a meaningful message to the user.  This
suggests that the database connection should be established somewhere
within Publisher.try_publish (or methods it calls).

--
Joel Shprentz              (703) 478-9668
1516 Park Glen Court       jshprentz@his.com
Reston, VA 20190




reply