commit_on_success 如何处理嵌套?

发布于 2024-10-15 09:24:19 字数 533 浏览 3 评论 0原文

我对在特定情况下应该如何处理交易感到有点困惑。

我有一些代码可以归结为:

from django.db import transaction

@transaction.commit_on_success
def process_post():
    #do stuff with database
    for reply in post_replies:
        process_post_reply(reply)

@transaction.commit_on_success
def process_post_reply(reply):
    #do stuff with database

我想知道如果 process_post_reply() 失败会发生什么。

commit_on_success 如何处理嵌套?它是否会理解提交每个 process_post_reply() 或者如果失败则整个 process_post_reply() 会回滚?

I'm a bit confused about how I should handle transactions in a particular situation.

I've got some code that boils down to this:

from django.db import transaction

@transaction.commit_on_success
def process_post():
    #do stuff with database
    for reply in post_replies:
        process_post_reply(reply)

@transaction.commit_on_success
def process_post_reply(reply):
    #do stuff with database

I want to know what happens if a process_post_reply() fails.

How does commit_on_success handle being nested? Will it understand to commit each process_post_reply() or if one fails the whole process_post() rolls back?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠北森屋 2024-10-22 09:24:19

这是它的源代码: https:// github.com/django/django/blob/1.2.4/django/db/transaction.py#L286

enter_transaction_management 就像将新的事务处理模式放在线程堆栈上一样简单。

因此,在您的情况下,如果 process_post_reply() 失败(即发生异常),则事务将全部回滚,然后异常从 process_post() 向上传播> 也一样,但没有什么可以回滚的。

不,如果一个 process_post_reply() 失败,则整个 process_post() 不会回滚 - 那里没有魔法,只有数据库级别的 COMMIT 和 ROLLBACK ,这意味着回滚的只是最后一次提交process_post_reply()之后写入数据库的内容。

总结一下,我认为您需要的只是围绕process_post的一个commit_on_success(),可能由事务保存点 - 不幸的是,仅在 PostgreSQL 后端可用,尽管 MySQL 5.x 支持它们以及。

编辑 2012 年 4 月 10 日:对 MySQL 的 Savepoint 支持现已在 Django 1.4 中可用

编辑 2014 年 7 月 2 日:事务管理已在 Django 1.6 中完全重写 - https://docs.djangoproject.com/en/1.6/topics/db/transactions/commit_on_success 已被弃用。

Here's the source code of it: https://github.com/django/django/blob/1.2.4/django/db/transaction.py#L286

And enter_transaction_management is as simple as putting new transaction handling mode on the thread stack.

So, in your case, if process_post_reply() fails (i.e. exception occurs), then the transaction is rolled back in its entirety, and then the exception propagates upwards from process_post() as well but there is nothing to roll back.

And no, if one process_post_reply() fails then the whole process_post() is not being rolled back - there's no magic there, only COMMIT and ROLLBACK on the database level, which means that what gets rolled back is only what has been written to the DB after the last commited process_post_reply().

Summarizing, I think that what you need is just a single commit_on_success() around process_post, possibly supported by transaction savepoints - which unfortunately are available only in PostgreSQL backend, even though MySQL 5.x supports them as well.

EDIT 10 Apr 2012: Savepoint support for MySQL is now available in Django 1.4

EDIT 2 Jul 2014: Transaction management has been completely rewritten in Django 1.6 - https://docs.djangoproject.com/en/1.6/topics/db/transactions/ and commit_on_success has been deprecated.

誰認得朕 2024-10-22 09:24:19

为了更好地控制事务管理,最好使用transaction.commit_manually():

@transaction.commit_on_success
def process_post(reply):
    do_stuff_with_database()
    for reply in post_replies:
        process_post_reply(transaction_commit_on_success=False)

def process_post_reply(reply, **kwargs):
    if kwargs.get('transaction_commit_on_success', True):
        with transaction.commit_manually():
            try:
                do_stuff_with_database()
            except Exception, e:
                transaction.rollback()
                raise e
            else:
                transaction.commit()
    else:
        do_stuff_with_database()

在这里您可以根据情况决定是否提交事务。

To gain more control on the transaction management, it's good to use transaction.commit_manually():

@transaction.commit_on_success
def process_post(reply):
    do_stuff_with_database()
    for reply in post_replies:
        process_post_reply(transaction_commit_on_success=False)

def process_post_reply(reply, **kwargs):
    if kwargs.get('transaction_commit_on_success', True):
        with transaction.commit_manually():
            try:
                do_stuff_with_database()
            except Exception, e:
                transaction.rollback()
                raise e
            else:
                transaction.commit()
    else:
        do_stuff_with_database()

Here you can decide depending on circumstances, commit transaction or not.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文