If a new record is to be added to the User table if the user doesn't already 'exist', should 'lock in share mode' (perhaps, 'select for update'?) be used to prevent another thread from trying to do the same thing? By 'exist', it could just be a record with the same username. For example, I'm trying to make sure all users have a unique username, so I'm checking whether any existing record with that username can be retrieved. If not, it can go ahead. If several threads are trying to add in the same username, there'd be a race condition.
Perhaps, would it be a good idea to rely on the unique constraint and catch IntegrityError
? That is, upon an IntegrityError, it'd read the table again and pick out the record that'd have just been added.
Thanks!