介绍

函数说明

from django.db import transaction
transaction.atomic  # 原子性操作,把一系列操作当做一个整体,错了则集体回退
transaction.on_commit(func,using = None) # 在整个事件完成后,执行func函数

使用方法


from django.db import transaction
# atomic
@transaction.atomic
def index(request):
... def index(request):
with transation.atomic():
....
.... # on_commit
@transaction.atomic
def index(request):
do_something...
transation.oncommit(func)

示例

from django.db import models

class Account(models.Model):
nid = models.IntegerField(primary_key=True)
money = models.IntegerField(verbose_name="余额")

models.py

from django.contrib import admin
from django.urls import path,re_path,include
from app01 import views urlpatterns = [
re_path("index.html",views.index)
]

urls.py

1、正常情况,转账成功

from django.shortcuts import render,HttpResponse
from app01 import models
from django.db.models import F def index(request):
models.Account.objects.filter(nid=1).update(money=F("money")-1) # 账号1给账号2转账1元钱
# raise IndexError
models.Account.objects.filter(nid=2).update(money=F("money")+1)
return HttpResponse("OK")

views.py

2、转账到一半出现错误

from django.shortcuts import render,HttpResponse
from app01 import models
from django.db.models import F def index(request):
models.Account.objects.filter(nid=1).update(money=F("money")-1)
raise IndexError
models.Account.objects.filter(nid=2).update(money=F("money")+1)
return HttpResponse("OK")

views.py

3、还原所有money到1000,配置事务

from django.shortcuts import render,HttpResponse
from app01 import models
from django.db import transaction
from django.db.models import F def index(request):
with transaction.atomic():
models.Account.objects.filter(nid=1).update(money=F("money")-1)
raise IndexError
models.Account.objects.filter(nid=2).update(money=F("money")+1)
return HttpResponse("OK") # 可以使用装饰器
# @transaction.atomic
# def index(request):
# models.Account.objects.filter(nid=1).update(money=F("money")-1)
# raise ValueError("出错")
# models.Account.objects.filter(nid=2).update(money=F("money")+1)
# return HttpResponse("OK")

views.py

4、验证on_commit

说明:如果原子性操作出错,则整个on_commit都不会执行,无论on_commit放在什么位置。这与直接在该位置执行func是不一样的。

from django.shortcuts import render,HttpResponse
from app01 import models
from django.db import transaction
from django.db.models import F def before():
print("before")
# 可以做一些特定工作,如发邮件、记录log def after():
print("after") @transaction.atomic
def index(request):
transaction.on_commit(before)
models.Account.objects.filter(nid=1).update(money=F("money")-1)
# raise ValueError("出错")
models.Account.objects.filter(nid=2).update(money=F("money")+1)
transaction.on_commit(after)
return HttpResponse("OK")

views.py

from django.shortcuts import render,HttpResponse
from app01 import models
from django.db import transaction
from django.db.models import F def before():
print("before")
# 可以做一些特定工作,如发邮件、记录log def after():
print("after") @transaction.atomic
def index(request):
transaction.on_commit(before)
models.Account.objects.filter(nid=1).update(money=F("money")-1)
raise ValueError("出错")
models.Account.objects.filter(nid=2).update(money=F("money")+1)
transaction.on_commit(after)
return HttpResponse("OK")

触发错误,raise ValueError,未出现func的东西

Django_事务的更多相关文章

  1. Spring基于AOP的事务管理

                                  Spring基于AOP的事务管理 事务 事务是一系列动作,这一系列动作综合在一起组成一个完整的工作单元,如果有任何一个动作执行失败,那么事务 ...

  2. SQLServer事务同步下如何收缩日志

    事务同步是SQLServer做读写分离的一种常用的方式. 随着业务数据的不断增长,数据库积攒了大量的日志,为了腾出硬盘空间,需要对数据库日志进行清理 订阅数据库的日志清理 因为订阅数据库所有的数据都来 ...

  3. 事务日志已满,原因为“ACTIVE_TRANSACTION”

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 异常处理汇总-数据库系列  http://www.cnblogs.com/dunitia ...

  4. Mysql事务探索及其在Django中的实践(二)

    继上一篇<Mysql事务探索及其在Django中的实践(一)>交代完问题的背景和Mysql事务基础后,这一篇主要想介绍一下事务在Django中的使用以及实际应用给我们带来的效率提升. 首先 ...

  5. Mysql事务探索及其在Django中的实践(一)

    前言 很早就有想开始写博客的想法,一方面是对自己近期所学知识的一些总结.沉淀,方便以后对过去的知识进行梳理.追溯,一方面也希望能通过博客来认识更多相同技术圈的朋友.所幸近期通过了博客园的申请,那么今天 ...

  6. CRL快速开发框架系列教程七(使用事务)

    本系列目录 CRL快速开发框架系列教程一(Code First数据表不需再关心) CRL快速开发框架系列教程二(基于Lambda表达式查询) CRL快速开发框架系列教程三(更新数据) CRL快速开发框 ...

  7. 玩转spring boot——结合JPA事务

    接着上篇 一.准备工作 修改pom.xml文件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=&q ...

  8. MySQL 系列(三)你不知道的 视图、触发器、存储过程、函数、事务、索引、语句

    第一篇:MySQL 系列(一) 生产标准线上环境安装配置案例及棘手问题解决 第二篇:MySQL 系列(二) 你不知道的数据库操作 第三篇:MySQL 系列(三)你不知道的 视图.触发器.存储过程.函数 ...

  9. PHP中PDO事务的使用方法

    事务 (Transaction) 是操作数据库中很重要的一个功能, 它可以让你预定一条, 或者一系列 SQL 语句, 然后一起执行. 在执行的过程中, 如果其中的某条执行失败, 可以回滚所有已更改的操 ...

随机推荐

  1. P1169 [ZJOI2007]棋盘制作

    题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...

  2. MVC下载文档

    public FileStreamResult DownFile(string content,string name) { ProInterface.ISubject ems = new ProSe ...

  3. The Gene of Bitizens

    1.          Summary The document is about the general idea of the architecture design of the Bitizen ...

  4. hadoop在CentOS下的安装配置

    版本:CentOS-6.8-x86_64-minimal,hadoop2.6.4,jdk1.7.0 首先把jdk.hadoop压缩包下载发送到CentOS下并解压 下载发送不多赘述,解压命令tar - ...

  5. 【转】Autofac高级用法之动态代理

    原文:http://www.cnblogs.com/stulzq/p/8547839.html 前言 Autofac的DynamicProxy来自老牌的Castle项目.DynamicProxy(以下 ...

  6. JavaWeb基础—HTML小结

    ---是什么?超文本标记语言---能干什么?描述网页的一种语言---怎么干?一套标签 前端三剑客的关系: 1. HTML是网页内容的载体. 2. CSS样式是表现. 3. JavaScript是用来实 ...

  7. PostgreSQL的xlog实验一

    磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL基础知识与基本操作索引页     回到顶级页面:PostgreSQL索引页 一,从initdb开始,initdb执行结束 ...

  8. codevs 2639 约会计划

    codevs 2639 约会计划 题目描述 Description cc是个超级帅哥,口才又好,rp极高(这句话似乎降rp),又非常的幽默,所以很多mm都跟他关系不错.然而,最关键的是,cc能够很好的 ...

  9. 3 CRM 销售与客户 我的客户,公共客户池

    1.销售与客户的表结构 1.公共客户与我的客户 ---公共客户(公共资源) 1.没有报名 2.3天没有跟进 3.15天没有成单 客户分布表 龙泰 男 yuan 2018-5-1 3天未跟进 龙泰 男 ...

  10. P4360 [CEOI2004]锯木厂选址

    P4360 [CEOI2004]锯木厂选址 这™连dp都不是 \(f_i\)表示第二个锯木厂设在\(i\)的最小代价 枚举1号锯木厂 \(f_i=min_{0<=j<i}(\sum_{i= ...