From 12130c800663cd8419bf48937802e70e5ef143bd Mon Sep 17 00:00:00 2001 From: Eric Fischer Date: Mon, 30 Nov 2015 08:59:55 -0500 Subject: [PATCH] Fixing CohortMembership and ATOMIC_REQUESTS issue What had been happening was related to the fact that ATOMIC_REQUESTS is now on, preventing writing to the database until a request is completed. The problem with that was that CohortMemberships, with their explicit transaction handling, were breaking that rule and touching the database. The CohortMembership needs to also touch the database to update the underlying course_user_groups information, to prevent the database from getting into a strange state. After this commit, every time a CohortMembership is updated or created, the corresponding course_user_groups are also updated and explicitly written to the database. This occurs atomically with the CohortMembership, and does not wait for the end of the request. --- openedx/core/djangoapps/course_groups/models.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/openedx/core/djangoapps/course_groups/models.py b/openedx/core/djangoapps/course_groups/models.py index 3fa34f0f47..7543666fb8 100644 --- a/openedx/core/djangoapps/course_groups/models.py +++ b/openedx/core/djangoapps/course_groups/models.py @@ -98,9 +98,12 @@ class CohortMembership(models.Model): success = False for __ in range(max_retries): + # This block will transactionally commit updates to CohortMembership and underlying course_user_groups. with transaction.atomic(): try: + # This block is atomic in order to make CohortMembership creation a small operation. + # It will commit the creation of the new CohortMembership, if needed. with transaction.atomic(): saved_membership, created = CohortMembership.objects.select_for_update().get_or_create( user__id=self.user.id, @@ -123,9 +126,11 @@ class CohortMembership(models.Model): self.previous_cohort_name = saved_membership.course_user_group.name self.previous_cohort_id = saved_membership.course_user_group.id self.previous_cohort.users.remove(self.user) + self.previous_cohort.save() saved_membership.course_user_group = self.course_user_group self.course_user_group.users.add(self.user) + self.course_user_group.save() super(CohortMembership, saved_membership).save(update_fields=['course_user_group'])