On 09/23/2013 12:27 AM, Aymeric Augustin wrote:
On 22 sept. 2013, at 23:09, Shai Berger <s...@platonix.com> wrote:

The code base I mentioned earlier relies mostly on TransactionMiddleware, but
does have some explicit commit_on_success's etc. (it's Django 1.4 based, so no
@atomic or anything like it).
I'm genuinely interested in making it as easy as possible to switch to the new
transaction management.

If you weren't using the transaction middleware, in 1.6 you would be in
autocommit mode and we wouldn't have this discussion at all.

Once you switch to ATOMIC_REQUESTS, assuming we go with the current
plan — and it still looks like the best compromise at this point — you'll have
to insert an extra atomic block to guarantee a clean rollback.

try:
     do_stuff_in_db()
except DatabaseError:
     do_more_stuff_in_db()

must be changed to:

try:
     with transaction.atomic():
         do_stuff_in_db()
except DatabaseError:
     do_more_stuff_in_db()

As explained in the documentation, this pattern has the advantage of delimiting
clearly what gets rolled back when an exception happens, even if the code is
extended or refactored. It's explicit and I like it.
And, if savepoints need to be avoided for some reason you can use:

try:
    do_stuff_in_db()
except DatabaseError:
    connection.set_rollback(False)
    do_more_stuff_in_db(

This will lead to the code working as it has worked before. The set_rollback() method is documented here: https://docs.djangoproject.com/en/dev/topics/db/transactions/#django.db.transaction.set_rollback

And, please, do not say this is about DatabaseErrors. That is not true. I added a couple of tests to demonstrate this: https://github.com/akaariai/django/commit/530ba8ae599c9510f9dc6433658f88b1988754d5. The added tests fail when patched, but they would fail even worse on master - the calls to save() / create() in exception blocks will succeed, but the operations are silently rolled back. I don't think Django 1.6 should be released with this behaviour.

  - Anssi

--
You received this message because you are subscribed to the Google Groups "Django 
developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to