def get_or_create_tracker(self, user, topic): is_new = True sid = transaction.savepoint(using=self.db) try: with get_atomic_func()(): obj = TopicReadTracker.objects.create(user=user, topic=topic) transaction.savepoint(sid) except DatabaseError: transaction.savepoint_rollback(sid) obj = TopicReadTracker.objects.get(user=user, topic=topic) is_new = False return obj, is_new
def get_or_create_tracker(self, user, forum): """ Correctly create tracker in mysql db on default REPEATABLE READ transaction mode It's known problem when standrard get_or_create method return can raise exception with correct data in mysql database. See http://stackoverflow.com/questions/2235318/how-do-i-deal-with-this-race-condition-in-django/2235624 """ is_new = True sid = transaction.savepoint(using=self.db) try: with get_atomic_func()(): obj = ForumReadTracker.objects.create(user=user, forum=forum) transaction.savepoint_commit(sid) except DatabaseError: transaction.savepoint_rollback(sid) is_new = False obj = ForumReadTracker.objects.get(user=user, forum=forum) return obj, is_new