Monday, March 29, 2021

Trouble with Select_for_update. Postgresql

Hi there . code is as follows:
`
models.Classroom table have just one record.
# THREAD 1
school_classrooms = models.Classroom.objects.select_for_update().all()
with transaction.atomic():
list(school_classrooms)
time.sleep(10)

# THREAD 2
classroom = models.Classroom.objects.first()
classroom.name = 'somename'
classroom.save()
`
thread 1 starts. then thread 2 starts to update the only Classroom record but because the record locked by table 1  , is will wait 10 seconds . then thread 1 releases the lock so thread 2 starts to update and exits with no error. surprisingly thread 2 didn't do anything at all and the Classroom record does not updated . How is is possible and how to solve that ? please help. thanks

database: Postgres in docker / latest version
django: django 3
disclaimer: thread 1 performed with a postman request to my api and thread 2 performed by django shell.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/593dfed2-f942-43b0-8faf-a6f13a56f257n%40googlegroups.com.

No comments:

Post a Comment