Share
Embed
Transcript
18 slides
-
Django ORM In A Glance • Access DB row like
an simple object • obj.attribute, • obj.save() • Aware of Relational DB’s Advantage • Use Transaction to support Atomic and Isolation -
Transaction • Within transaction • You can rollback your data
while meets problem • You can isolate what you see and what you write -
However • Default isolation level is read committed. • PostgreSQL
default use auto-commit mode + read commiteed(So As Django) -
Read Committed • Data commit In another transaction will be
seen by current transaction -
The problem is • Your logic base on first query’s
result but it will be changed latter -
Two Possible Solution • Change the isolation level to Serializable
• Use Advisory Lock -
Serializable • Each Serializable Transaction touch the same data will
execute one by one. -
Show Me The Code counter = 0 # logically we
would like a infinite retry. but infinite is too scaring while counter < 1000: counter += 1 try: with transaction.atomic(): cursor = connections[‘your_db’].cursor() cursor.execute('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE') # Your DB Operations except OperationalError: time.sleep(counter) # to avoid retry in a very short interval continue 1 2 3 -
Show Me The Code counter = 0 # logically we
would like a infinite retry. but infinite is too scaring while counter < 1000: counter += 1 try: with transaction.atomic(): cursor = connections[‘your_db’].cursor() cursor.execute('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE') # Your DB Operations except OperationalError: time.sleep(counter) # to avoid retry in a very short interval continue 1 2 3 1 the context manager will start a transaction and commit after exists the block -
Show Me The Code counter = 0 # logically we
would like a infinite retry. but infinite is too scaring while counter < 1000: counter += 1 try: with transaction.atomic(): cursor = connections[‘your_db’].cursor() cursor.execute('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE') # Your DB Operations except OperationalError: time.sleep(counter) # to avoid retry in a very short interval continue 1 2 3 2 change the isolation level to serializable -
Show Me The Code counter = 0 # logically we
would like a infinite retry. but infinite is too scaring while counter < 1000: counter += 1 try: with transaction.atomic(): cursor = connections[‘your_db’].cursor() cursor.execute('SET TRANSACTION ISOLATION LEVEL SERIALIZABLE') # Your DB Operations except OperationalError: time.sleep(counter) # to avoid retry in a very short interval continue 1 2 3 3 latter transaction will meets Operational Error, usually it needs retry -
• Distinguish different lock with different lock id lock_id =
"Any String You Want" with advisory_lock(lock_id=lock_id, shared=False): pass # Your DB Operations Here -
• advisory_lock is a third party package. • https://django-mysql.readthedocs.io/en/latest/locks.html •
https://github.com/Xof/django-pglocks lock_id = "Any String You Want" with advisory_lock(lock_id=lock_id, shared=False): pass # Your DB Operations Here -
Conclusion • The default parameter for Django may not fit
the strict requirement of concurrency • Read about isolation and atomic concept for a complex application -
Reference • Advisory locks https://learning.oreilly.com/library/view/ mastering-postgresql-96/9781783555352/cd02e6cb- 53c6-4bfc-8b12-b7cbb6e6dda7.xhtml • Isolation level
https://learning.oreilly.com/library/view/ postgresql-development-essentials/9781783989003/ ch05.html • Yuren 好⽂文 https://medium.com/getamis/database- transaction-isolation-a1e448a7736e