[Tutor] Exceptions while dealing with MySQL

Kent Johnson kent37 at tds.net
Sat Apr 21 16:36:02 CEST 2007


Thanos Panousis wrote:
> Hello list,
> 
> I am developing a network management system that relies heavily on a
> MySQL database. The logic of the program is unavoidably coupled with
> query results I get from various tables.
> 
> This is OK as long the mysql server has 100% uptime, but it hardly
> does. Say that I have to make at least 20 different kinds of queries
> here and there in different parts of my code(all together more than a
> thousand queries). What is the best approach to make error checking
> possible, with the least amount of code duplication?
> 
> Right now my code is probably ill-structured in terms of the database
> backend. A MySQLdb.connection object is juggled around as an arguement
> to my own program's objects, so that they can retrieve the info they
> need from the database tables when needed. After a whole program
> iteration has finished, the connection object is destroyed and a new
> one is cerated for the next iteration.
> 
> This means that a 5-minute downtime of the SQL server will cause my
> program to crash. It is really too much trouble checking for
> exceptions every time I make a simple query. What approach should I
> try take?

What do you want to have happen when the server is down? Should the 
program wait and retry?

One possibility would be to wrap the database connection in a class 
which checks for exceptions and retries. If you are only doing queries 
(no updates) this might work OK. Or perhaps the wrapper could test for 
availability of the database before making a request.

Kent


More information about the Tutor mailing list