Path: csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!feeder1-2.proxad.net!proxad.net!feeder1-1.proxad.net!hq-usenetpeers.eweka.nl!hq-usenetpeers.eweka.nl!bcyclone03.am1.xlned.com!bcyclone03.am1.xlned.com!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'explicitly': 0.05; 'postgresql': 0.07; 'happens.': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'python': 0.11; 'wrote': 0.14; 'posted': 0.15; 'detects': 0.16; 're-read': 0.16; 'really?': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 50': 0.16; 'statements,': 0.16; 'followed': 0.16; 'commit': 0.19; 'result.': 0.19; 'thu,': 0.19; 'starts': 0.20; 'seems': 0.21; 'select': 0.22; 'commands,': 0.24; 'logical': 0.24; 'specifies': 0.24; 'specify': 0.24; 'earlier': 0.24; 'header:X -Complaints-To:1': 0.27; 'statement': 0.30; 'lines': 0.31; 'there.': 0.32; 'another': 0.32; 'maybe': 0.34; "can't": 0.35; 'connection': 0.35; 'transaction': 0.35; 'no,': 0.35; 'point.': 0.35; 'but': 0.35; 'there': 0.35; '+0200,': 0.36; 'should': 0.36; 'list': 0.37; 'nov': 0.38; 'same.': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'even': 0.60; 'new': 0.61; 'first': 0.61; 'confirm': 0.64; 'different': 0.65; 'here': 0.66; 'between': 0.67; 'frank': 0.68; 'default': 0.69; 'repeat': 0.74; 'commands.': 0.84; 'confirms': 0.95; 'state.': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Db transactions and locking Date: Fri, 28 Nov 2014 07:44:45 +0200 References: <4loe7at2ls7tfq0oe041ru9svvsm8aksik@4ax.com> X-Gmane-NNTP-Posting-Host: 197.86.210.253 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 75 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417153499 news.xs4all.nl 2924 [2001:888:2000:d::a6]:55343 X-Complaints-To: abuse@xs4all.nl X-Received-Body-CRC: 3264062325 X-Received-Bytes: 5254 Xref: csiph.com comp.lang.python:81666 "Dennis Lee Bieber" wrote in message news:4loe7at2ls7tfq0oe041ru9svvsm8aksik@4ax.com... > On Thu, 27 Nov 2014 12:24:39 +0200, "Frank Millman" > declaimed the following: > > >>All Python database adaptors that I have used start a transaction when you >>open a cursor. I have just re-read DB-API 2.0, and I cannot see anything >>that specifies this behaviour, but AFAICT this is what happens. >> > > Really? > Well, I can't prove it, no, but *something* starts a transaction, even if you do not specify one. Maybe the adaptor detects the first statement after opening a cursor, and starts a transaction at that point. Here is my empirical 'proof' - I start up a PostgreSQL interactive session with psql, and list the current locks - there are 3, which always seem to be there. >From another psql session, I issue some sql commands. Here is a list of the commands, followed by the number of current locks. SELECT * FROM mytable - 3 BEGIN - 3 SELECT * FROM mytable - 4 (a new AccessShareLock on mytable) COMMIT - 3 This confirms what I posted earlier - "PostgreSQL by default commits between each statement unless you explicitly start a transaction." Then I start a python session, set up a connection using psycopg2, and do the same. cur = conn.cursor() - 3 cur.execute('SELECT * FROM mytable') - 4 cur.fetchall() - 4 cur.close() - 4 conn.commit() - 3 This seems to confirm what I thought, but then I continued, and was surprised at the result. I can repeat these lines at will - cur.execute('SELECT * FROM mytable') - 4 conn.commit() - 3 But if I do this - cur.execute('SELECT * FROM mytable') - 4 cur.execute('commit') - 3 cur.execute('SELECT * FROM mytable') - 3 cur.execute('commit') - 3 There seems to be a difference between conn.commit() and cur.execute('commit'), which leaves the connection in a different state. However, for my purposes, this is academic. The main lesson I have learned is that you should always issue a commit after any logical set of SQL statements, even if they are only SELECTs, otherwise the locks are not released. Frank