इस सरल उदाहरण पर विचार करें:Django
# a bank account class
class Account:
@transaction.commit_on_success
def withdraw(self, amount):
# code to withdraw money from the account
@transaction.commit_on_success
def add(self, amount):
# code to add money to the account
# somewhere else
@transaction.commit_on_success
def makeMoneyTransaction(src_account, dst_account, amount):
src_account.withdraw(amount)
dst_account.add(amount)
(https://code.djangoproject.com/ticket/2227 से लिया गया)
एक अपवाद Account.add()
में जन्म देती हैं, तो Account.withdraw()
में लेन-देन अभी भी प्रतिबद्ध है और पैसे की जाएगी खो जाएगा, क्योंकि Django वर्तमान में नेस्टेड लेनदेन संभाल नहीं करता है।
Django को पैच लगाने के बिना, हम कैसे सुनिश्चित कर सकते हैं कि प्रतिबद्धता डेटाबेस को भेजी जाती है, लेकिन केवल तभी जब @transaction.commit_on_success
सजावट के तहत मुख्य कार्य अपवाद उठाए बिना खत्म हो जाता है?
मैं इस स्निपेट में आया: http://djangosnippets.org/snippets/1343/ और ऐसा लगता है कि यह काम कर सकता है। क्या कोई कमी है मुझे पता होना चाहिए कि क्या मैं इसका उपयोग करता हूं?
यदि आप मदद कर सकते हैं तो अग्रिम में बहुत धन्यवाद।
पीएस मैं कॉपी करने कर रहा हूँ पहले से दृश्यता के प्रयोजनों के लिए कोड का टुकड़ा उद्धृत:
def nested_commit_on_success(func):
"""Like commit_on_success, but doesn't commit existing transactions.
This decorator is used to run a function within the scope of a
database transaction, committing the transaction on success and
rolling it back if an exception occurs.
Unlike the standard transaction.commit_on_success decorator, this
version first checks whether a transaction is already active. If so
then it doesn't perform any commits or rollbacks, leaving that up to
whoever is managing the active transaction.
"""
commit_on_success = transaction.commit_on_success(func)
def _nested_commit_on_success(*args, **kwds):
if transaction.is_managed():
return func(*args,**kwds)
else:
return commit_on_success(*args,**kwds)
return transaction.wraps(func)(_nested_commit_on_success)
मुझे यह भी यकीन नहीं है कि यह कोड स्निपेट काम करेगा। ऐसा लगता है कि अब 4 साल का है, और कोई कह रहा है कि यह यहां काम नहीं करता है: http://djangosnippets.org/snippets/2515/ – alexpirine