当前位置:网站首页>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 '
})
边栏推荐
- Attack and defense World Lottery
- 1024 | in the fourth year officially called Menon, the original intention is still there, and continue to move forward
- Using ecological power, opengauss breaks through the performance bottleneck
- 好吃难吃饱七分为宜;好喝难喝醉三分为佳
- 1178 questions of Olympiad in informatics -- ranking of grades
- Shenzhi Kalan Temple
- Attack and defense world MFW
- Local Oracle reported ora-12514: tns: the listener cannot recognize the requested service at present
- 带宽 与 货币
- MCDF top level verification scheme
猜你喜欢

MCDF top level verification scheme

Design and development of GUI programming for fixed-point one click query

如何在qsim查看软件对象的实例?

Risk control and application of informatization project
![Connection failed during installation of ros2 [ip: 91.189.91.39 80]](/img/7f/92b7d44cddc03c58364d8d3f19198a.png)
Connection failed during installation of ros2 [ip: 91.189.91.39 80]

Vcenter7.0 installation of ibm3650m4 physical machine

Flask request data acquisition and response

Redis configuration file download
![[netding cup 2020 rosefinch group]nmap 1 two solutions](/img/fa/b1349cb42b5768b7510217239ba73a.png)
[netding cup 2020 rosefinch group]nmap 1 two solutions

Hundreds of people participated. What are these people talking about in the opengauss open source community?
随机推荐
It's better to be full than delicious; It's better to be drunk than drunk
Realize SKU management in the background
Demo:st05 find text ID information
Node installation and debugging
Management of product pictures
The third letter to the little sister of the test | Oracle stored procedure knowledge sharing and test instructions
Graph node deployment and testing
Flask one to many database creation, basic addition, deletion, modification and query
ERP生产作业控制 华夏
UVM Introduction Experiment 1
Notes in "PHP Basics" PHP
Realize SPU management in the background
Vertical align cannot align the picture and text vertically
Weekly learning summary
Openresty + keepalived 实现负载均衡 + IPV6 验证
sql_ Mode strict mode (ansi/traditional/strict_trans_tables)
Breadth first search
Introduction to depth first search (DFS)
Containerd failed to pull private database image (kubelet)
Attack and defense world MFW