当前位置:网站首页>Transaction, order system add transaction
Transaction, order system add transaction
2022-07-27 08:32:00 【pink_ Pig___】
The isolation level of the transaction
serialize / Serialization (Serializable)
** characteristic :** Only one transaction can be executed at the same time , If the current transaction is not committed , You cannot start a new transaction .
advantage :
The highest degree of isolation 、 The best security
shortcoming :
Efficiency is too low
Repeatable (repeatable)[ Default isolation level ]
The user executes in the same transaction select The results are consistent , Not affected by other transaction modifications . in other words , When the user starts the transaction , Created a snapshot . No matter how other data changes , The contents of the snapshot remain unchanged
Read submitted (read committed)
Users are different in the same transaction select result , Affected by other transaction modifications . Other transactions must be submitted , Can affect the current transaction
Read uncommitted (read uncommitted)
User's in the current transaction select Affected by other transaction modifications , Even if other transactions are not committed , It can still be read by the current transaction .
advantage : Efficient
shortcoming : Dirty reading 、 Fantasy reading 、 It can't be read repeatedly
View transaction isolation level
Look at the big picture
mysql> select @@global.tx_isolation;
+-----------------------+
| @@global.tx_isolation |
+-----------------------+
| REPEATABLE-READ |
+-----------------------+
1 row in set (0.01 sec)
# MySQL8.0+ How to write it
# select @@global.transaction_isolation;
View the current isolation level
mysql> select @@tx_isolation;
+-----------------+
| @@tx_isolation |
+-----------------+
| REPEATABLE-READ |
+-----------------+
1 row in set (0.00 sec)
# MySQL8.0+ How to write it
# select @@transaction_isolation;
What is dirty reading
Took out the data that the database should not have
What is unreal reading
The extracted data is not consistent with the actual data in the database
Create order add transaction
class Goodsview(APIView):
# Generate order
def post(self,request):
# Judgement login
user = request.META.get('USER') # Get the login after middleware detection , The user information passed 、
if not user:
return Response({
'code': 400,
'msg': ' The user is not logged in '
})
user_id = user['user_id']
addr_id=request.data.get('addr_id')
goods_list=request.data.get('goods_list') #[{'goods_id':1,'num':2},{'goods_id':2,'num':2}]
pay_method=request.data.get('pay_method')
# Get the shipping address and verify
addr_info=Addr.objects.filter(id=addr_id,user_id=user_id).first()
if not addr_info:
return Response({
'code':400,
'msg':' Receiving address does not exist '
})
# Get product information and verify
goods_buy=[]
for i in goods_list:
goods_info=Goods.objects.filter(id=i['goods_id']).first()
if not goods_info:
continue
if goods_info.stock<i['num']:
return Response({
'code':400,
'msg':'%s The stock of goods is insufficient '%goods_info.sku_name
})
goods_buy.append({
'id':i['goods_id'],
'num':i['num'],
'price':goods_info.selling_price
})
from django.db import transaction # Import the class of the transaction
with transaction.atomic(): # Open transaction
point=transaction.savepoint() # Add the rollback point of the transaction
try:
# Create order
order_info=Order.objects.create(
order_id=str(random.randint(100000,999999)),
user_id=user_id,
addr=addr_info,
total_price=0,
total_count=0,
pay_method=pay_method,
pay_status=1,
)
if not goods_buy:
transaction.savepoint_rollback(point)
return Response({
'code': 400,
'msg': ' Empty order '
})
# Create order items
for i in goods_buy:
# Create sub order
OrderGoods.objects.create(
goods_id=i['id'],
count=i['num'],
price=i['price'],
order=order_info,
)
# Modify master order
order_info.total_price+=(i['price']*i['num'])
order_info.total_count+=i['num']
# Deducting inventory
g_info=Goods.objects.filter(id=i['id']).first()
g_info.stock=i['num']
g_info.save()
order_info.save()
except:
transaction.savepoint_rollback(point) # Transaction rollback
return Response({
'code': 200,
'msg': ' Order failure '
})
# Commit transaction
transaction.savepoint_commit(point)
# Return to order success
return Response({
'code':200,
'msg':' checkout success '
})
边栏推荐
- 虚拟机克隆
- Forced login, seven cattle cloud upload pictures
- Attack and defense world MFW
- Redis configuration file download
- JS advanced knowledge - function
- MCDF top level verification scheme
- pytorch_demo1
- List删除集合元素
- "PHP Basics" tags in PHP
- Massive data Xiao Feng: jointly build and govern opengauss root community and share a thriving new ecosystem
猜你喜欢

One book 1201 Fibonacci sequence

P7 Day1 get to know the flask framework

On Valentine's day, I drew an object with characters!

Realization of background channel group management function

Functions and arrow functions

Bandwidth and currency

阿里云国际版回执消息简介与配置流程

Flask project configuration

Solution to the program design of the sequence structure of one book (Chapter 1)

说透缓存一致性与内存屏障
随机推荐
Luogu Taotao picks apples
Forced login, seven cattle cloud upload pictures
虚拟机克隆
Flask project configuration
CMD command and NPM command
Alibaba cloud international receipt message introduction and configuration process
Iterators and generators
Introduction to depth first search (DFS)
[geek challenge 2019] finalsql 1
Openresty + keepalived 实现负载均衡 + IPV6 验证
Netdata 性能监测工具介绍、安装、使用
Eval and assert execute one sentence Trojan horse
无法获取下列许可SOLIDWORKS Standard,无法找到使用许可文件。(-1,359,2)。
SSTI template injection
You may need an additional loader to handle the result of these loaders.
好吃难吃饱七分为宜;好喝难喝醉三分为佳
openGauss之TryMe初体验
Installation and use of Supervisor
UVM Introduction Experiment 1
Breadth first search