Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!xlned.com!feeder7.xlned.com!newsfeed.xs4all.nl!newsfeed4a.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.023 X-Spam-Evidence: '*H*': 0.95; '*S*': 0.00; 'finally:': 0.07; 'subject:help': 0.08; '"work': 0.09; 'cursor': 0.09; 'try:': 0.09; 'wrong,': 0.09; 'cc:addr:python-list': 0.11; 'python': 0.11; 'complexity,': 0.16; 'demonstrable': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'handler,': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'commit': 0.19; 'aug': 0.22; 'cc:addr:python.org': 0.22; 'helpful': 0.24; 'cc:2**0': 0.24; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'am,': 0.29; 'generally': 0.29; 'message-id:@mail.gmail.com': 0.30; 'gives': 0.31; 'code': 0.31; 'exceptions': 0.31; 'subject:some': 0.31; 'bugs': 0.33; 'transaction': 0.35; 'something': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'skip:u 10': 0.60; 'easy': 0.60; 'simple,': 0.60; 'new': 0.61; "you're": 0.61; 'back': 0.62; "you'll": 0.62; 'such': 0.63; 'happen': 0.63; 'more': 0.64; 'connection.': 0.74; 'hanging': 0.84; 'opens': 0.91; 'to:none': 0.92; 'connection,': 0.95 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=/m0chh+kfgFryvIas3JgrVCTZ/8WAp6RXDTd3ePkgBc=; b=MLQmXOmBpeosocAhW+XpT3Qque/ZhJjiN6rmeTTUeABYXVmZJP0SukUrboSmhUFbdP RS6VGV/ZGnvfpcWxjbhD4RrG5cUKMtp4McAQ7xxRElh2MMWKLUNy+3QSK/ksdSi9RgQZ ZiXFZa8mAzokceodo7AhENzuWxI18muMV7rCbiltNVvjujzE+UfGfl4H76x0cPbs7tBt BwUz9z6ZavKQti57nhAOBoT9pzjl3TWEwR0EKg/8VbLIVGpHNOULT1tHB+Z52pHdoACA 8QJLEb+U1iILqnD+2GBU+PjU2pV5vnbVFMvFYyn8Ujqz15B3CipOOGCQFeKJejd/y3mW ZuuA== MIME-Version: 1.0 X-Received: by 10.50.66.197 with SMTP id h5mr9984544igt.34.1407543204762; Fri, 08 Aug 2014 17:13:24 -0700 (PDT) In-Reply-To: References: Date: Sat, 9 Aug 2014 10:13:24 +1000 Subject: Re: Newbie needing some help From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 28 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1407543213 news.xs4all.nl 2927 [2001:888:2000:d::a6]:53308 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:75917 On Sat, Aug 9, 2014 at 9:55 AM, Chris Kaynor wrote: > try: > action > commit > finally: > rollback If commit/rollback automatically opens a new transaction, this would just roll back an empty transaction - not a big deal. But yes, I do see what you're looking at here. However, structures like this are necessary only if you're hanging onto the database connection. Python gives you a well-defined unhandled-exception handler, and it's easy to just let exceptions happen - if something goes wrong, you won't commit, and you'll get a helpful traceback on the console. My recommended model for Python databasing is: Create database connection, get cursor while "work to do": do work Commit Until such time as you have a demonstrable need for more complexity, this model is safe, simple, and easy to work with. And less code generally means less bugs :) ChrisA