[DB-SIG] Database connections don't stay alive

Chris Cogdon chris at cogdon.org
Fri Jun 3 19:56:47 CEST 2005

On Jun 3, 2005, at 10:50, Chris Cogdon wrote:

> The 'problem' here is that the database object goes out of scope when
> the function exits. When 'out of scope' objects get cleaned up is
> fairly implementation dependant, which is likely why it was working
> with MySQL.

Oops... sorry to respond to myself here :)

In the MySQL case, the cursor is probably keeping the database 
connection object alive, and THAT is why it works in MySQL. It may well 
be that the pyPgSQL cursor object is NOT connected to the database 
object through python, so when the database object goes out of scope, 
the reference count goes to zero, and it's cleaned up. On second 
thoughts, this DOES seem a little odd.

However, You really shouldn't be throwing the database object away (or 
trying to) since you need it in order to do your db.commit() and 
db.rollback() instructions.

Yes, enabling autocommit will alleviate the need for this, but you'll 
find that as your database application grows, you'll want to start 
doing more than one instruction atomically. And... its easier to get 
into the habit of wrapping your transactions in a try/except block now.

    ("`-/")_.-'"``-._        Chris Cogdon <chris at cogdon.org>
     . . `; -._    )-;-,_`)
    (v_,)'  _  )`-.\  ``-'
   _.- _..-_/ / ((.'
((,.-'   ((,/   fL

More information about the DB-SIG mailing list