#43 garbage collection of opened connections

MySQLdb-0.9.2
closed
Andy Dustman
MySQLdb (285)
5
2012-09-19
2002-10-08
Anonymous
No

When I open a connection
c = MySQLdb.connect(...parameters...)
inside a function, I expect that when I exit from the
function the connection is closed. This is not true at
all!
Just tested on a RedHat 7.3 with installed Python2.2.1
from python.org and MySQLdb-0.9.2

MySQLdb-0.9.0 worked fine, but I noticed a lot of
changes between those two versions!

Hope you can solve it as soon as possible... thanks!

Discussion

  • Logged In: YES
    user_id=168769

    Yup, this was a problem for me as well.

    I didn't bother to check if there was something wrong with
    the reference counting, since I haven't taken the time to
    familiarize myself with the Python/C API, but this quick fix
    did the trick for me.

    Insert at line 47 in MySQLdb/cursors.py:
    self.connection.close()

    I strongly suspect that this is not a 'clean' fix, as there
    mey be multiple cursors using the same connections. In my
    case thats not an issue but YMMV.

    cheers.

     
  • Dave Peticolas
    Dave Peticolas
    2003-06-13

    Logged In: YES
    user_id=9778

    The fix will close the connection, but the connection
    won't be garbage collected, so there is still a memory leak.

     
  • Dave Peticolas
    Dave Peticolas
    2003-06-13

    Logged In: YES
    user_id=9778

    Actually, I was wrong, calling close will result in the
    connection
    being garbage collected. The reason is that close() will call
    _mysql_ConnectionObject_clear which clears the
    'converter' attribute which has up to two references
    to bound methods which refer to the connection object.