https://docs.djangoproject.com/en/3.0/topics/db/transactions/

You may need to manually revert model state when rolling back a transaction.

The values of a model’s fields won’t be reverted when a transaction rollback happens. This could lead to an inconsistent model state unless you manually restore the original field values.

For example, given MyModel with an active field, this snippet ensures that the ifobj.active check at the end uses the correct value if updating active to True fails in the transaction:

from django.db import DatabaseError, transaction

obj = MyModel(active=False)
obj.active = True
try:
with transaction.atomic():
obj.save()
except DatabaseError:
obj.active = False if obj.active:
...

id 不连续



User.objects.get(x=y)注意这种写法 当且仅当需要x=y的情况下才不会抛异常 exception ;raise ParamError(CustomerError.X_NOT_EXIST)
可以使得事务回滚;但是 不raise error 不会使得事务回滚。

回滚 导致 自增id不连续。



Avoid catching exceptions inside atomic! You may need to manually revert model state when rolling back a transaction. 避免异常程序不抛错误 回滚 导致 自增id不连续。的更多相关文章

  1. Django1.8文档阅读手记

    主要集中在新特性上. Django1.8好像开始推荐使用python3 Django的QuerySet是延迟加载的,这个文档里面一般会明言,通过Connection SQL显示测试,外键对象也是延迟加 ...

  2. Thinking in Java,Fourth Edition(Java 编程思想,第四版)学习笔记(十二)之Error Handling with Exceptions

    The ideal time to catch an error is at compile time, before you even try to run the program. However ...

  3. TIJ——Chapter Twelve:Error Handling with Exception

    Exception guidelines Use exceptions to: Handle problems at the appropriate level.(Avoid catching exc ...

  4. {Django基础六之ORM中的锁和事务}一 锁 二 事务

    Django基础六之ORM中的锁和事务 本节目录 一 锁 二 事务 一 锁 行级锁 select_for_update(nowait=False, skip_locked=False) #注意必须用在 ...

  5. Django基础六之ORM中的锁和事务

    一 锁 行级锁 select_for_update(nowait=False, skip_locked=False) #注意必须用在事务里面,至于如何开启事务,我们看下面的事务一节. 返回一个锁住行直 ...

  6. day 71 Django基础六之ORM中的锁和事务

    Django基础六之ORM中的锁和事务   本节目录 一 锁 二 事务 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 锁 行级锁 select_for_update(no ...

  7. 08.Django基础六之ORM中的锁和事务

    一 锁 行级锁 select_for_update(nowait=False, skip_locked=False) #注意必须用在事务里面,至于如何开启事务,我们看下面的事务一节. 返回一个锁住行直 ...

  8. day 58 Django基础六之ORM中的锁和事务

      Django基础六之ORM中的锁和事务   本节目录 一 锁 二 事务 三 xxx 四 xxx 五 xxx 六 xxx 七 xxx 八 xxx 一 锁 行级锁 select_for_update( ...

  9. orm 锁 和 事务

    一 锁 锁的语句 select_for_update() 原生sql语句select * from t1 for update django 语句models.T1.objects.select_fo ...

随机推荐

  1. ubuntu20部署php-swoole开发环境

    第1步:安装依赖 add-apt-repository ppa:ondrej/php apt install php-dev 第2步:编译安卓swoole wget https://codeload. ...

  2. 【命令】man命令帮助文档详解

    前言:Linux命令分为内建命令和外部命令:内建命令是shell本身自带的,外部命令是是一个可执行程序 我们在使用命令帮助的时候需要钱哦区分命令是内建命令还是外部命令 一.查看一个命令是内建命令还是外 ...

  3. 【Tomcat】Tomcat服务器核心配置说明及标签

    目录 一,主要标签结构 二,Server标签 标签属性: 子标签: 三,Service 标签 子标签: 四,Executor 标签 属性: 五,Connector标签 属性: 六,Engine标签 属 ...

  4. 毕业三年,如何达到月薪30K?我想跟你聊聊!!

    写在前面 很多读者私信问我,自己工作三多年了,随着工作年限的不断增长,感觉自己的技术水平与自己的工作年限严重不符.想跳槽出去换个新环境吧,又感觉自己的能力达不到心仪公司的标准,即使投了简历也没人来通知 ...

  5. 云服务器部署LAMP

    一.安装Apache 1.安装httpd服务: sudo yum install httpd 2.开启服务: sudo systemctl start httpd 3.访问服务器IP成功显示Testi ...

  6. java集合大总结

    Java集合大总结 java集合框架简图(API关系图): 虚线框表示接口,实线框表示类. 特点和使用总结: Collection: 单列单值. List: 有序(查询顺序和插入顺序一致),有下标(索 ...

  7. Eclipse中,No compiler is provided in this environment. Perhaps you are running on a JRE rather than a

    问题说明 Eclipse导入Maven项目后,执行 mvn clean install后,出现如下错误: [INFO] ---------------------------------------- ...

  8. react脚手架抽离webpack报错解决

    我们在写react项目的时候,可能原有的webpack配置不满足我们的需求,需要自己去配置,可是你在创建脚手架的时候并不能在外部找到webpack文件,脚手架的webpack文件在node_modul ...

  9. Service Mesh——微服务中的流量管理中间件

    Service Mesh--微服务中的流量管理中间件 摘自-https://zhuanlan.zhihu.com/p/28794062 Service mesh 与 Cloud Native Kube ...

  10. Linux 时间同步 03 ntpdate时间同步

    Linux 时间同步 03 ntpdate时间同步 目录 Linux 时间同步 03 ntpdate时间同步 安装ntpdate 修改/etc/sysconfig/ntpdate 使用ntpdate手 ...