Hello, first, let me give you an idea of the problem I'm facing. This is a company with a fruit packaging plant, they use several printers for the barcodes they attach in fruit boxes, clamshells and pallets. There is a Django table that keeps track of the current number for each one (caja, clamshell and pallet):

They don't want to be holes in the middle, if the current number for a box is 00071799 then there must be boxes from 00000001 to 00071798. These numbers are used as primary keys for the Caja, Clamshell and Pallet models.
The printing process is using atomic transactions:
@transaction.atomic
def print_barcodes(request):
tid = transaction.savepoint()
# lot of code here
if error:
transaction.savepoint_rollback(tid)
# ...
return JsonResponse(response)
We are getting a lot of these:
_mysql_exceptions.IntegrityError: (1062, "Duplicate entry 'XXXXXXXXXX' for key 'PRIMARY'")
Which means we have a fault in the process or maybe we are improperly using transactions. Help will be much appreciated regarding the problem and the following questions:
- If we print 10 boxes from 00000001 to 00000010 and say (exaggeratedly) each box takes 1 second to print. Then, another guy wants to print boxes while the first transaction is still active, will he get 00000011? Considering that the numbers are updated during the transaction.
- Do transactions stack and lock the tables?
Thanks a lot