Hello,

I'm using Django since a couple of years and the more I use it the
more I wish a support for django-managed nested transactions.
In complex programs happens often to need them.

So I would post here my own code of such tool, based of Django 1.3
commit_on_success().
Please help understand if it's correct or not ;)

BEWARE: currently I used it only in simple tests !! Do not use in
production !

Hints, suggestions, critiques are most welcome !

Many thanks

Ale




from django.db.transaction import is_managed,managed,
enter_transaction_management, is_dirty, rollback, commit,
leave_transaction_management,_transaction_func
import threading

_tl = threading.local()

def nested_commit_on_success(using=None):

    def entering(using):
      lev = getattr(_tl,"level",0)
      lev += 1
      setattr(_tl,"level",lev)
      if lev >= 2: #is_managed():
        print "IN-NESTED"
      else:
        print "IN-NORMAL"
        enter_transaction_management(using=using)
        managed(True, using=using)

    def exiting(exc_value, using):
      lev = getattr(_tl,"level")
      oldlev = lev
      lev -= 1
      setattr(_tl,"level",lev)
      if oldlev >= 2:
        print "OUT-NESTED"
        return
      print "OUT-NORMAL"
      try:
          if exc_value is not None:
              if is_dirty(using=using):
                  rollback(using=using)
          else:
              if is_dirty(using=using):
                  try:
                      commit(using=using)
                  except:
                      rollback(using=using)
                      raise
      finally:
          leave_transaction_management(using=using)

    return _transaction_func(entering, exiting, using)

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to