[Zope-dev] Database Cursors and Gargabe collection ?

Romain Slootmaekers romain@zzict.com
Sun, 08 Sep 2002 18:06:30 +0200


Yo,

we have a problem using garbage collection to clean up open database 
cursors. Our approach is really simple, but doesn't work. :(

we have a method( on a SimpleItem object) getCursor:

def getCursor(self):
         """
         doc
         """
         if not hasattr(self,'_v_cursor'):
                 from MySQLdb import connect,escape_string
                 db=connect(...)
                 cursor=db.cursor()
                 self._v_cursor=CursorWrapper(cursor)
         cursor=self._v_cursor.getCursor()
         return cursor

the cursorwrapper is a class that closes the cursor on GC.

class CursorWrapper:
     def __init__(self,cursor):
         print "created"
         self.__cursor=cursor

     def getCursor(self):
         print "given out"
         return self.__cursor

     def __del__(self):
         self.__cursor.close()
         del self.__cursor
         print "deleted"

So in theory, the cursor will be closed on GC.

In practice however, we get multiple 'created' a lot of 'given out' but 
no 'deleted' messages in the cucu debugging.

After searching the web/zope site the only related thing we could find 
was a quote from Jim: "old objects don't die, they just fade away'
not very useful.

So the questions are:
1) why doesn't the __del__ get called on our wrapper ?
2) do we have to resort to closing and opening database cursors every 
single time we access the RDB ?


TIA,

Sloot.