Best practice for managing connections? - Mailing list psycopg

From Julian
Subject Best practice for managing connections?
Date
Msg-id 51626B31.60408@internode.on.net
Whole thread Raw
List psycopg
Hi,
I currently have something like this and it works well enough.

conn = None
try:
   conn = psycopg2.connect(...)
   curs = conn.cursor()
   curs.execute('...')
   conn.commit()
   curs.close()
   return 'success'
except:
   if conn:
      conn.rollback()
   return 'fail'

Now using the with statement support in psycopg2.5:

try:
    with psycopg2.connect(...) as conn:
        with conn.cursor() as curs:
            curs.execute('...')
    return 'success'
except:
    return 'fail'

Makes it alot easier. I'm assuming there will be no problems using the
same process with psycopg2.pool classes and connection objects?

I've taken a break for 3 weeks (my model m broke - now I own 4 - and
then got the flu). So rewriting some code will be good refresh, although
as mentioned in a previous post, I lack faith that it is actually doing
the right thing, any tips on that? (aside from monitoring the database
itself.)

Regards.
Jules.


psycopg by date:

Previous
From: Julian
Date:
Subject: Re: Psycopg 2.5 released
Next
From: Daniele Varrazzo
Date:
Subject: Re: Psycopg 2.5 released