Login

Tag "transaction"

Snippet List

nested transactions context manager and decorator

This is a modification of Django 1.3's transaction.commit_on_success() decorator and context manager. It's inspired by snippet [1343](http://djangosnippets.org/snippets/1343/) which unfortunately don't work in current Django neither as a context manager. In my junior projects it works fine but I've not tested in critical projects yet! YMMV ! How it works: it simply counts the nesting level and does the real transaction enter/exit only on first call and last call respectively (code copied from Django's commit_on_success() ). It use thread local storage to save the per-thread nesting level count safely. To use it just put the code in a file (i.e. nested_commit_on_success.py) and import and use it exacly as normal commit_on_success(), both as decorator (@nested_commit_success) or context manager (with nested_commit_on_success(): ). Any feedback is welcome!

  • decorator
  • nested
  • transaction
  • contextmanager
Read More

Username filled automatically with id

I thought this code for insert automatically id in username field. This method should be used in save method. This code work on a dbms that support transactions ( example: mysql+innodb or postgresql ). Let me know what you think about this snippet and if you advice an alternative solution by commenting below. Thanks :)

  • username
  • id
  • transaction
Read More

Nested commit_on_success

This is a simple modification to the standard transaction.commit_on_success decorator that is aware of existing transactions. If a managed transaction is already active then the wrapped function is called directly, assuming the active transaction will clean up as appropriate. Otherwise, a standard commit_on_success is performed. I'm using this to wrap the save() method of models that manipulate other related models on save, e.g: @nested_commit_on_success def save(self): super(MyClass,self).save() for m in other_models: self.fix_up_other_model(m) m.save()

  • transaction
Read More
Author: rfk
  • 2
  • 1

5 snippets posted so far.