Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #102783
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Chris Angelico <rosuav@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: Handling transactions in Python DBI module |
| Date | Thu, 11 Feb 2016 16:14:10 +1100 |
| Lines | 39 |
| Message-ID | <mailman.30.1455167660.22075.python-list@python.org> (permalink) |
| References | <92D3C964-0323-46EE-B770-B89E7E7E6D36@ravnalaska.net> <n9h4s7$aqb$1@ger.gmane.org> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8 |
| X-Trace | news.uni-berlin.de THVWbur75eyeOTrMyILeLwT8mRkw5ksnY3PK7OiDTjgg== |
| Return-Path | <rosuav@gmail.com> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'correspond': 0.07; 'performs': 0.07; 'api': 0.09; 'cc:addr :python-list': 0.09; 'cursor': 0.09; 'statements': 0.09; 'subject:module': 0.09; 'applies': 0.15; 'subsequent': 0.15; 'thu,': 0.15; 'weird': 0.15; '"python': 0.16; "'begin": 0.16; "'not',": 0.16; '2016': 0.16; 'block),': 0.16; 'conn': 0.16; 'conn,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'nest': 0.16; 'personally,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'statement.': 0.16; 'wrote:': 0.16; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'select': 0.23; 'bit': 0.23; 'feb': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; "i've": 0.25; "doesn't": 0.26; 'message-id:@mail.gmail.com': 0.27; 'executing': 0.27; 'succeed': 0.29; 'reset': 0.29; 'starts': 0.29; '(including': 0.30; 'connection': 0.30; 'transaction': 0.30; 'code': 0.30; 'checks': 0.30; "i'd": 0.31; 'guess': 0.31; 'statement': 0.32; 'structure': 0.34; 'received:google.com': 0.35; 'next': 0.35; 'but': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'possible': 0.36; 'basic': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'skip:p 20': 0.38; 'end': 0.39; 'does': 0.39; 'rather': 0.39; 'easy': 0.60; 'your': 0.60; 'close': 0.61; 'within': 0.64; 'state,': 0.66; 'therefore': 0.67; 'frank': 0.72; "'with'": 0.84; 'chrisa': 0.84; 'connection).': 0.84; 'to:none': 0.91 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=Lx46ZyF/4wxLTXxzl+5aB9nglvY6TaGtJzWIvLU1KkY=; b=AzLtyGU/Zti8Pe4Mmf4lKTdnY5pXb1Q55mMADHOrVYRNGb6fWyHaM0kvh9K3tRHNlI Sk430TnOZb6mepAUgxe04nC3aXsQDrVWC3GO1L8ctr/yoAGiEo5xQDgzjp8hm8T8F0jd ig59henPgw6E+bXjN37I6/z29DIu5wqVrtoJhy+bDsIqdHuw5uJ0lpG/p8UwYUSjnzTO GfYteT6DLZ+TpMmGPvRMu9erhzS0aL1igE55WCsdjdWKbXcbHyBLxkz4TO2ogXhqGs1B M2HFltC2VIBmKKUU3x1+xAvF0TjUmK0JsmsPcfSV25LUoqAXWjD5Y7+YKjMx7wxqB+uf KVFw== |
| X-Google-DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:cc:content-type; bh=Lx46ZyF/4wxLTXxzl+5aB9nglvY6TaGtJzWIvLU1KkY=; b=aI6oRmcWAnow2KT8oyS8GZwoAGDfZwcbqjdS/z4xP0/QcXY7xSrPV4pjqJOK1DB1AH L2TxP4ds2dyKDhpljzVex1qLIUFffly74kUEH2u3BUKLlu6F/oqe6J0UQdcM0A0+QXIM 5Fwtj0p3JrXHWTu58FUcBC7GY7EaEsoVNb4sWLXER29xHp2fqhT/TTF/vHlr0H3831CE az3CbKL7/rkLfJ2nv3CmYl3TWA/wZmg6UA9sa97nzNDYdVtoti4wnGAZZ9Gc9ru74Yyp sBkTvZSNUZR0WMlC49Lgchwxoxq1ffjfhzSJTcSlnF2dbr1v4yoE4ojDRxYjUIysqGkd sLjw== |
| X-Gm-Message-State | AG10YORxGwCcFZaaXJOB/p4kkDtWjEL78aCpXGJN/WT19uYZDO8M3nYJo+DkH4boMmqeLv+D8uzItG7DmVBCvg== |
| X-Received | by 10.50.176.195 with SMTP id ck3mr15009161igc.94.1455167651026; Wed, 10 Feb 2016 21:14:11 -0800 (PST) |
| In-Reply-To | <n9h4s7$aqb$1@ger.gmane.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.21rc2 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:102783 |
Show key headers only | View raw
On Thu, Feb 11, 2016 at 4:06 PM, Frank Millman <frank@chagford.com> wrote:
> A connection has 2 possible states - 'in transaction', or 'not in
> transaction'. When you create the connection it starts off as 'not'.
>
> When you call cur.execute(), it checks to see what state it is in. If the
> state is 'not', it silently issues a 'BEGIN TRANSACTION' before executing
> your statement. This applies for SELECT as well as other statements.
>
> All subsequent statements form part of the transaction, until you issue
> either conn.commit() or conn.rollback(). This performs the required action,
> and resets the state to 'not'.
>
> I learned the hard way that it is important to use conn.commit() and not
> cur.execute('commit'). Both succeed in committing, but the second does not
> reset the state, therefore the next statement does not trigger a 'BEGIN',
> with possible unfortunate side-effects.
When I advise my students on basic databasing concepts, I recommend
this structure:
conn = psycopg2.connect(...)
with conn, conn.cursor() as cur:
cur.execute(...)
The transaction block should always start at the 'with' block and end
when it exits. As long as you never nest them (including calling other
database-using functions from inside that block), it's easy to reason
about the database units of work - they always correspond perfectly to
the code blocks.
Personally, I'd much rather the structure were "with
conn.transaction() as cur:", because I've never been able to
adequately explain what a cursor is/does. It's also a bit weird that
"with conn:" doesn't close the connection at the end (just closes the
transaction within that connection). But I guess we don't need a
"Python DB API 3.0".
ChrisA
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: Handling transactions in Python DBI module Chris Angelico <rosuav@gmail.com> - 2016-02-11 16:14 +1100
csiph-web