Skip to content

Instantly share code, notes, and snippets.

@jezhalford
Created August 6, 2015 13:42
Show Gist options
  • Save jezhalford/187d546f6ed5227dca0b to your computer and use it in GitHub Desktop.
Save jezhalford/187d546f6ed5227dca0b to your computer and use it in GitHub Desktop.
Cause a MySQL Deadlock
<?php
/*
SQL -
CREATE TABLE innodb_deadlock_maker(a int primary key) engine=innodb;
*/
$dbA = new DB(); // Get two instances of whatever class you use to handle DB connections
$dbB = new DB(); // and do any initialisation you need
$dbA->connect(); // and open two separate connections
$dbB->connect(); //
// Then run some SQL using both...
$dbA->sql('set transaction isolation level serializable');
$dbB->sql('set transaction isolation level serializable');
$dbA->sql('delete from innodb_deadlock_maker');
$dbA->sql('insert into innodb_deadlock_maker set a = 2');
$dbA->sql('start transaction');
$dbA->sql('update innodb_deadlock_maker set a = 2 WHERE a = 2');
$dbB->sql('start transaction');
$dbB->sql('update innodb_deadlock_maker set a = 14 WHERE a = 2');
$dbB->sql('COMMIT');
$dbA->sql('COMMIT');
// DEADLOCK!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment