[Zope-DB] DA's violating transactional assertions

Dieter Maurer dieter@handshake.de
Thu, 23 Jan 2003 21:14:59 +0100


Recently, I saw several DA's that contains the following functionality
in their "query" method:

   ....
   try:
     ...
     execute SQL command
     ...
   except <some transient exceptions>:
     close database connection
     reopen databse connection
     retry query

While this is *very* fine from a practical point of view (it
recovers from most transient database problems), in invalidates
Zope's transactional assertions.

Assume the following case:

       Zope request start (Zope transaction begins)

       1. SQL Method - succeeds

       2. SQL Method - transient exception
          this will close the database connection
	  and thereby performing an implicit transaction rollback
	  (for this DA)
	  Retry succeeds

       Zope request ends (Zope transaction committed)

The transactional assertion (all or nothing) may be violated
for Zope's transaction, as it may contain the effect
of the 2. SQL Method call but have lost that of the first
one.


DA's should handle transient exceptions not by retrying the
the query themselves but by raising a (special) exception
derived from "ConflictError" after they closed and reopened the
connection.
This will cause Zope to abort its transaction and then
retry its complete transaction again.



Dieter