当前位置:网站首页>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 '
})
边栏推荐
- 2022-07-26 group 4 abstract classes and interfaces
- [netding cup 2020 Qinglong group]areuserialz (buuctf)
- Realization of background channel group management function
- Teach you to build a nail warning robot hand in hand
- "PHP Basics" PHP statements and statement blocks
- sql_ Mode strict mode (ansi/traditional/strict_trans_tables)
- Development of three database general SQL code based on PG Oracle and MySQL
- Forced login, seven cattle cloud upload pictures
- SSTI template injection
- Background image related applications - full, adaptive
猜你喜欢

好吃难吃饱七分为宜;好喝难喝醉三分为佳

Hundreds of people participated. What are these people talking about in the opengauss open source community?

Installation and use of beef XSS

openGauss之TryMe初体验
![[geek challenge 2019] finalsql 1](/img/a7/857d47639fcb38e0055a2444206b8c.png)
[geek challenge 2019] finalsql 1

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

情人节,我用字符画出了一个对象!

Use of "PHP Basics" Boolean

regular expression

缓存一致性与内存屏障
随机推荐
Day3 -- flag state holding, exception handling and request hook
idea远程调试
Chapter 2 foreground data display
vCenter7.0管理Esxi7.0主机
Block, there is a gap between the block elements in the row
阿里云国际版回执消息简介与配置流程
Realize SPU management in the background
UVM Introduction Experiment 1
Ubuntu: install PostgreSQL
Graph node deployment and testing
Fluent rendering mechanism - GPU thread rendering
Installation and use of beef XSS
如何在qsim查看软件对象的实例?
Plato farm is expected to further expand its ecosystem through elephant swap
Using ecological power, opengauss breaks through the performance bottleneck
Realization of backstage brand management function
如何卸载--奇安信安全终端管理系统
All in one 1319 - queue for water
Create a project to realize login and registration, generate JWT, and send verification code
Functions and arrow functions