[Twisted-Python] Exception handling in t.e.adbapi

Don Dwiggins ddwiggins at advpubtech.com
Wed Nov 19 19:38:54 EST 2008

I've been using adbapi (with pyodbc talking to MS SQL Server) 
successfully in a Windows service.  I've discovered that occasionally, 
the network between the service and the database machine goes down and 
comes back up (either that or the database server itself cycles down and 
back up).  When this happens, any pyodbc connections hanging around are 
corrupted, so that the next query gets an exception (class 
pyodbc.Error).  Currently, the best thing I can do is to restart the 

I'm trying to rewrite the service to be able to catch, analyze, and 
respond to exceptions from pyodbc.  In particular, in the case above, 
it's possible to reconnect and retry the query.  If the network 
connection is still down, I'd like to log it, send a message to alert 
someone, or whatever.

I've rewritten a ConnectionPool.runQuery call to use runInteraction to 
call a function (in a thread) that works with the cursor created in 
runInteraction to execute the query.  The function wraps the 
cursor.execute(...) in a try-except.  I can successfully catch the 
exception, and I try to recover by doing cursor.reconnect(), then 
cursor.reopen(), and finally cursor.execute(...) again.  I'm finding 
that this fails with a "wrong connection for thread" exception.  I 
haven't been able to figure out why that should happen; more 
importantly, I'm not sure that I'm going about this in the right way. 
Any insights or pointers to code that does this kind of thing would be 

Don Dwiggins
Advanced Publishing Technology

More information about the Twisted-Python mailing list