Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Israel Brewster Newsgroups: comp.lang.python Subject: Re: Handling transactions in Python DBI module Date: Thu, 11 Feb 2016 08:05:31 -0900 Lines: 79 Message-ID: References: <92D3C964-0323-46EE-B770-B89E7E7E6D36@ravnalaska.net> Mime-Version: 1.0 (Mac OS X Mail 9.1 \(3096.5\)) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable X-Trace: news.uni-berlin.de BtRD+7vrzLPE+j8ZXlU8GwejR5ZHniDutbkloHPPb4YQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.003 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'modify': 0.04; 'subject:Python': 0.05; 'performs': 0.07; 'api': 0.09; 'cc:addr :python-list': 0.09; 'cursor': 0.09; 'executes': 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; 'called,': 0.16; 'confusion': 0.16; 'modules,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'skip:n 50': 0.16; 'statement.': 0.16; 'wrote:': 0.16; 'implementing': 0.18; 'module,': 0.18; 'library': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(the': 0.22; 'commands,': 0.22; 'implicit': 0.22; 'cc:no real name:2**0': 0.22; 'select': 0.23; '(or': 0.23; 'feb': 0.23; 'wrote': 0.23; 'this:': 0.23; 'second': 0.24; '(this': 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'handling': 0.27; 'least': 0.27; 'executing': 0.27; 'finally,': 0.27; 'object,': 0.27; 'specifically': 0.28; 'helpful.': 0.29; 'issues.': 0.29; 'issuing': 0.29; 'succeed': 0.29; 'reset': 0.29; 'starts': 0.29; "i'm": 0.30; 'connection': 0.30; 'url:mailman': 0.30; 'transaction': 0.30; 'checks': 0.30; 'primary': 0.31; 'statement': 0.32; 'though,': 0.32; 'url:python': 0.33; 'hopefully': 0.33; 'open': 0.33; "i'll": 0.33; 'url:listinfo': 0.34; 'case,': 0.34; 'add': 0.34; 'on,': 0.35; 'next': 0.35; 'could': 0.35; 'quite': 0.35; 'but': 0.36; 'there': 0.36; 'url:org': 0.36; 'possible': 0.36; 'modules': 0.36; 'pm,': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'thanks': 0.37; 'charset:us-ascii': 0.37; 'itself': 0.38; 'skip:p 20': 0.38; 'goes': 0.39; 'does': 0.39; 'rather': 0.39; 'url:mail': 0.40; 'well.': 0.40; 'called': 0.40; 'some': 0.40; 'questions': 0.40; 'your': 0.60; 'close': 0.61; 'header:Message-Id:1': 0.61; 'ago.': 0.61; 'received:10.9': 0.66; 'state,': 0.66; 'statement,': 0.66; 'results': 0.66; 'potentially': 0.67; 'therefore': 0.67; 'subject': 0.70; 'connection,': 0.72; 'frank': 0.72; 'race': 0.72; 'carried': 0.76; 'connection.': 0.76; 'sounds': 0.76; 'hoping': 0.77; 'received:12': 0.81; 'open,': 0.84; 'response,': 0.95 X-Warning: RFC compliance checks disabled due to whitelist X-Warning: Reverse-Path DNS check skipped due to whitelist X-Warning: Maximum message size check skipped due to whitelist X-Warning: Realtime Block Lists skipped due to whitelist X-Warning: System filters skipped due to whitelist X-Warning: Domain filters skipped due to whitelist X-Warning: User filters skipped due to whitelist X-Warning: Anti-Spam check skipped due to whitelist X-Whitelist: 2147483645 X-Envelope-From: israel@ravnalaska.net X-Envelope-To: frank@chagford.com In-Reply-To: X-Mailer: Apple Mail (2.3096.5) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.21rc2 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102817 On Feb 10, 2016, at 8:06 PM, Frank Millman wrote: >=20 > "Israel Brewster" wrote in message = news:92D3C964-0323-46EE-B770-B89E7E7E6D36@ravnalaska.net... >=20 >> 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: >>=20 >> - 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. >>=20 >> 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? >>=20 >> 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? >>=20 >> Finally, how do other DB API modules, like psycopg2, ensure that = ROLLBACK is called if the user never explicitly calls close()? >=20 > 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. >=20 > 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. >=20 > A connection has 2 possible states - 'in transaction', or 'not in = transaction'. When you create the connection it starts off as 'not'. >=20 > 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. >=20 > 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'. >=20 > 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. Thanks - that is actually quite helpful. So the way I am looking at it = now is that the connection would have an internal cursor as I suggested. = =46rom your response, I'll add a "state" flag as well. If the state flag = is not set when execute is called on a cursor, the cursor itself will = start a transaction and set the flag (this could happen from any cursor, = though, so that could potentially cause a race condition, correct?). In = any case, there is now a transaction open, until such a time as commit() = or rollback() is called on the connection, or close is called, which = executes a rollback(), using the connection's internal cursor. Hopefully that all sounds kosher.=20 >=20 > HTH >=20 > Frank Millman >=20 >=20 > --=20 > https://mail.python.org/mailman/listinfo/python-list