Hello! Thank you for answering. So if that's the case then what is happening is that (according to question 1) the second transaction is getting the same number as the first one and complaining with the IntegrityError while trying to save. Is that correct?
On Wednesday, October 21, 2015 at 10:21:50 AM UTC-5, esauro wrote: > > Hi, > Transactions are a different concept than locks. In few words a > transaction means that the all the sentences within it are done or not > done, but can't be half done (unless you have savepoints, and want to > rollback to a particular point). The locks avoid other for making > operations on the table or row which should prevent from race conditions as > no one is allowed to do modifications or even reads until the operation > finished. > > So the answer to to your first questions is no, if you are using id > generation from DB it would take the first free at the moment it runs. > > I think the problem you have is that the id generation is outside > database, and probably is based on what is on the DB when you start > printing (would be nice if you put more code about id generations). > > Regards, > Esau Rodriguez. > > On Wed, Oct 21, 2015 at 4:02 PM, César Bustíos Benites < > cesarb...@gmail.com <javascript:>> wrote: > >> Anyone? T_T >> >> On Tuesday, October 20, 2015 at 1:02:27 PM UTC-5, César Bustíos Benites >> wrote: >>> >>> 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): >>> >>> >>> <https://lh3.googleusercontent.com/-dmAawvjfCg4/ViZuR_tPlvI/AAAAAAAAY08/zD0pPBWATeE/s1600/series.png> >>> >>> 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: >>> >>> 1. 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. >>> 2. Do transactions stack and lock the tables? >>> >>> Thanks a lot >>> >>> >>> >>> >>> >>> -- >> 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...@googlegroups.com <javascript:>. >> To post to this group, send email to django...@googlegroups.com >> <javascript:>. >> Visit this group at http://groups.google.com/group/django-users. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/django-users/b43184ad-cc06-4244-aea8-624a325c3538%40googlegroups.com >> >> <https://groups.google.com/d/msgid/django-users/b43184ad-cc06-4244-aea8-624a325c3538%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> >> For more options, visit https://groups.google.com/d/optout. >> > > > > -- > Esaú Rodríguez > esa...@gmail.com <javascript:> > -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/cdd5d1ae-4736-4fe3-9052-00eceaf044b4%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.