Best practice for managing connections?

From: Julian <tempura(at)internode(dot)on(dot)net>
To: "psycopg(at)postgresql(dot)org" <psycopg(at)postgresql(dot)org>
Subject: Best practice for managing connections?
Date: 2013-04-08 07:01:05
Message-ID: 51626B31.60408@internode.on.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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.

Browse psycopg by date

  From Date Subject
Next Message Daniele Varrazzo 2013-04-08 07:04:36 Re: Psycopg 2.5 released
Previous Message Julian 2013-04-08 00:57:38 Re: Psycopg 2.5 released