Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I'm currently trying to handle concurrent inserts correctly using MySQL, I'm basically doing the following:

CREATE TABLE foo (id INT NOT NULL PRIMARY KEY) ENGINE=INNODB;

CREATE TABLE bar (id INT NOT NULL PRIMARY KEY) ENGINE=INNODB;

BEGIN; -- process 1
BEGIN; -- process 2

INSERT INTO foo VALUES(1); -- process 1
INSERT INTO foo VALUES(2); -- process 2

SAVEPOINT save_a; -- process 1
SAVEPOINT save_b; -- process 2

SELECT * FROM bar WHERE id = 42 FOR UPDATE; -- process 1 -> empty
SELECT * FROM bar WHERE id = 42 FOR UPDATE; -- process 2 -> empty

INSERT INTO bar VALUES(42); -- process 1 -> OK
INSERT INTO bar VALUES(42); -- process 2 -> ERROR 1213 (40001): Deadlock found when trying to get lock; try restarting transaction

COMMIT; -- process 1

The first process runs as expected and creates entry 1 in foo and 42 in bar

The second process rolls back the whole transaction automatically, so value 2 in foo is lost

Is there any way to not rollback the transaction on deadlock?

So in this case I can issue a ROLLBACK TO SAVEPOINT save_b

share|improve this question

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.