Skip to content

Instantly share code, notes, and snippets.

@meech-ward
Created March 24, 2021 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save meech-ward/b4e10089e1f76a26da8c651c93fc799e to your computer and use it in GitHub Desktop.
Save meech-ward/b4e10089e1f76a26da8c651c93fc799e to your computer and use it in GitHub Desktop.
Phantom Read MySQL
CREATE DATABASE bank;
USE bank;

CREATE TABLE accounts (
  id integer PRIMARY KEY AUTO_INCREMENT,
  `name` VARCHAR(255),
  balance DECIMAL(10,2)
);

INSERT INTO accounts (`name`, balance)
VALUES
('Sam', 100),
('Patrick', 100);

Session 1

start transaction;
select * from accounts; 

That should show 2 accounts

Session 2

insert into accounts (name, balance)
values ("Jeremy", 100);

Session 1

update accounts set balance = 0;
select * from accounts;

That should still show 2 accounts, but now there's three, even though it's inside of a repeatable read transaction.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment