在 Django 中使用带有错误处理的原子块

Use atomic blocks with error handling in Django

我有一个 Django 1.9 应用程序,它是 运行 一段代码,其中根据对某些远程 API 的查询结果对数据库进行更改。例如,这可能是关于提交、文件更改、审阅者、拉取请求等的数据,我想将其作为实体保存在我的数据库中。

commit_data = commit_API_client.get_commit_info(argument1, argument2)
new_commit = models.Commit.Create(**commit_data)

#if the last API failed, this will fail
#I will need to run this again to get these files, so I need
#to get the commit all over again, too
files = file_API_client.get_file_info(new_commit.id)
new_files = models.Files.Create(**files)

#do some more stuff here 

我调用的少数 API 之一很可能会 return 一些错误而不是有效数据。我基本上需要将此部分变成一个原子事务,这样如果 HTTP requests 没有错误 return,我将所有更改提交到数据库。否则,如果 2 个 API return 正确,但第 3 个不正确,我可能会丢失一些数据。

我看到 Django 支持 commit hooks 数据库事务,但我想知道这是否适用于这种情况以及我将如何实现它。

如果你想把它变成一个原子操作,只需将它包装在transaction.atomic()中。如果您的任何代码引发异常,整个块将回滚。如果您只是从远程 API 进行读取,那应该可以正常工作。一个典型的模式是:

def my_function():
    try:
        with transaction.atomic():
            # Do your work here
            pass
    except Exception:
        # Do some error handling
        pass

Django 确实有一个 on_commit hook,但它在这里并不适用。其目的是在交易成功完成后 运行 一些代码。例如,如果交易成功,您可以使用它来将一些日志数据写入远程 API。