Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #102784

Re: Handling transactions in Python DBI module

From "Frank Millman" <frank@chagford.com>
Newsgroups comp.lang.python
Subject Re: Handling transactions in Python DBI module
Date 2016-02-11 07:28 +0200
Message-ID <mailman.31.1455168546.22075.python-list@python.org> (permalink)
References <92D3C964-0323-46EE-B770-B89E7E7E6D36@ravnalaska.net> <n9h4s7$aqb$1@ger.gmane.org> <CAPTjJmphJvtCKUB6Qr-vp_1epEWxBgQxmfKEPMOhQp3pAPGG+A@mail.gmail.com>

Show all headers | View raw


"Chris Angelico"  wrote in message 
news:CAPTjJmphJvtCKUB6Qr-vp_1epEWxBgQxmfKEPMOhQp3pAPGG+A@mail.gmail.com...
>
> When I advise my students on basic databasing concepts, I recommend
> this structure:
>
> conn = psycopg2.connect(...)
>
> with conn, conn.cursor() as cur:
>     cur.execute(...)
>

Does this automatically issue a 'conn.commit()' on exit?

I have found that this is important, for the following reason.

If you issue a bare SELECT to PostgreSQL, it executes it without creating 
any locks. However, if it is inside a transaction, it does create a lock (I 
forget exactly which one).

Because psycopg2 silently executes 'BEGIN TRANSACTION', your SELECTs always 
happen inside a transaction whether you specify it or not. If you do not 
issue a conn.commit(), the locks do not get cleared.

Frank

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Handling transactions in Python DBI module "Frank Millman" <frank@chagford.com> - 2016-02-11 07:28 +0200

csiph-web