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


Groups > comp.lang.python > #102782

Re: Handling transactions in Python DBI module

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From "Frank Millman" <frank@chagford.com>
Newsgroups comp.lang.python
Subject Re: Handling transactions in Python DBI module
Date Thu, 11 Feb 2016 07:06:09 +0200
Lines 59
Message-ID <mailman.29.1455167184.22075.python-list@python.org> (permalink)
References <92D3C964-0323-46EE-B770-B89E7E7E6D36@ravnalaska.net>
Mime-Version 1.0
Content-Type text/plain; format=flowed; charset="iso-8859-1"; reply-type=original
Content-Transfer-Encoding 7bit
X-Trace news.uni-berlin.de DkMP+OUjYKu+W7CbCxFNRA8pDIAThhoLSK8XnEk1D1RA==
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'modify': 0.04; 'subject:Python': 0.05; 'performs': 0.07; 'api': 0.09; 'cursor': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'statements': 0.09; 'subject:module': 0.09; 'python': 0.10; 'appropriate': 0.14; 'applies': 0.15; 'explicitly': 0.15; 'subsequent': 0.15; "'begin": 0.16; "'not',": 0.16; 'confusion': 0.16; 'modules,': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'skip:n 50': 0.16; 'statement.': 0.16; 'implementing': 0.18; 'module,': 0.18; 'library': 0.20; '(the': 0.22; 'commands,': 0.22; 'implicit': 0.22; 'select': 0.23; '(or': 0.23; 'wrote': 0.23; 'this:': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'header:X-Complaints-To:1': 0.26; 'handling': 0.27; 'least': 0.27; 'executing': 0.27; 'finally,': 0.27; 'object,': 0.27; 'specifically': 0.28; 'issues.': 0.29; 'issuing': 0.29; 'succeed': 0.29; 'reset': 0.29; 'starts': 0.29; "i'm": 0.30; 'connection': 0.30; 'transaction': 0.30; 'checks': 0.30; 'primary': 0.31; 'statement': 0.32; 'open': 0.33; 'on,': 0.35; 'next': 0.35; 'but': 0.36; 'possible': 0.36; 'modules': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'skip:p 20': 0.38; 'goes': 0.39; 'does': 0.39; 'rather': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; 'some': 0.40; 'questions': 0.40; 'your': 0.60; 'close': 0.61; 'ago.': 0.61; 'state,': 0.66; 'statement,': 0.66; 'results': 0.66; 'therefore': 0.67; 'subject': 0.70; 'connection,': 0.72; 'frank': 0.72; 'carried': 0.76; 'connection.': 0.76; 'hoping': 0.77
X-Injected-Via-Gmane http://gmane.org/
X-Gmane-NNTP-Posting-Host 197.89.154.180
In-Reply-To <92D3C964-0323-46EE-B770-B89E7E7E6D36@ravnalaska.net>
X-MSMail-Priority Normal
Importance Normal
X-Newsreader Microsoft Windows Live Mail 15.4.3502.922
X-MimeOLE Produced By Microsoft MimeOLE V15.4.3502.922
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:102782

Show key headers only | View raw


"Israel Brewster"  wrote in message 
news:92D3C964-0323-46EE-B770-B89E7E7E6D36@ravnalaska.net...

> I am working on implementing a Python DB API module, and am hoping I can 
> get some help with figuring out the workflow of handling transactions. In 
> my experience (primarily with
> psycopg2) the workflow goes like this:
>
> - When you open a connection (or is it when you get a cursor? I *think* it 
> is on opening a connection), a new transaction is started
> - When you close a connection, an implicit ROLLBACK is performed
> - After issuing SQL statements that modify the database, you call commit() 
> on the CONNECTION object, not the cursor.
>
> My primary confusion is that at least for the DB I am working on, to 
> start/rollback/commit a transaction, you execute the appropriate SQL 
> statement (the c library I'm using doesn't
> have any transactional commands, not that it should). However, to execute 
> the statement, you need a cursor. So how is this *typically* handled? Does 
> the connection object keep an > internal cursor that it uses to manage 
> transactions?
>
> I'm assuming, since it is called on the connection, not the cursor, that 
> any COMMIT/ROLLBACK commands called affect all cursors on that connection. 
> Is that correct? Or is this DB
> specific?
>
> Finally, how do other DB API modules, like psycopg2, ensure that ROLLBACK 
> is called if the user never explicitly calls close()?

Rather than try to answer your questions point-by-point, I will describe the 
results of some investigations I carried out into this subject a while ago.

I currently support 3 databases, so I use 3 DB API modules - 
PostgreSQL/psycopg2, Sql Server/pyodbc, and sqlite3/sqlite3. The following 
applies specifically to psycopg2, but I applied the lessons learned to the 
other 2 as well, and have had no issues.

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.

HTH

Frank Millman

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:06 +0200

csiph-web