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


Groups > comp.lang.python > #75917

Re: Newbie needing some help

References <CANhUTb9tEnT=uTMhdA+Fd2zJTMauo1BWkX2dgcE0dG_H5arx9Q@mail.gmail.com> <CALvWhxtWyQw08TBVOTDhfFjm0=PP19QY3Yv-v74Q2T=ndyW91g@mail.gmail.com> <CAPTjJmq=dydhFEooRHqG2UaHTREjJSyqk9McuDYhc8PRjSzTgA@mail.gmail.com> <CALvWhxvx0HUQmMKTw=XCYac7E8ZsivM_S=3E-FAHfUpJT-Ge3g@mail.gmail.com>
Date 2014-08-09 10:13 +1000
Subject Re: Newbie needing some help
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.12773.1407543213.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Aug 9, 2014 at 9:55 AM, Chris Kaynor <ckaynor@zindagigames.com> 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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Newbie needing some help Chris Angelico <rosuav@gmail.com> - 2014-08-09 10:13 +1000

csiph-web