﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
21670	New Model.save() mechanism causes deadlocks in mysql transactions	err	nobody	"With default mysql isolation level (repeatable read).
For example I have this model:
                                                                                

{{{
class TestTable(models.Model):                                                  
    id = models.IntegerField(primary_key=True)  # not autoincrement                                  
    name = models.CharField(max_length=1024)
}}}

And this code

{{{
with transaction.atomic():
   m = TestTable()
   m.id = N
   m.save()
}}}


And when I have 2 parallel model.save() (with transaction.atomic) I'm immediately getting deadlocks.

for example:
transaction 1: 
set autocommit=0;UPDATE `test_table` SET `name` = '' WHERE `test_table`.`id` = 1;  # nothing updated. this update causes mysql gap locking

transaction 2:
set autocommit=0;UPDATE `test_table` SET `name` = '' WHERE `test_table`.`id` = 2; # nothing updated. this update causes mysql gap locking

transaction 1: 
INSERT INTO `test_table` (`id`, `name`) VALUES (1, ''); # waiting for lock

transaction 2:
INSERT INTO `test_table` (`id`, `name`) VALUES (2, '');
# boom. deadlock

I dont know the solution. Maybe recommeded mysql isolation level should be ""read commited"" or maybe old save mechanism should be used.





"	Bug	closed	Database layer (models, ORM)	1.6	Normal	wontfix		django@… Mathias Dannesbo	Accepted	0	0	0	0	0	0
