MySQL Concurrency, How Does It Work and Do I Need To Handle It in My Application - Stack Overflow
MySQL Concurrency, How Does It Work and Do I Need To Handle It in My Application - Stack Overflow
to handle it in my application
Asked 12 years, 11 months ago Modified 2 years, 7 months ago Viewed 27k times
I am currently running a MySQL database. All of my tables are using the Table
Engine InnoDB.
41
Everyone who logs into my application can view records and I am worried that at
some point two users might update or insert a record at the same time. Does
MySQL handle this type of concurrency issue gracefully, or is this something that I
am going to have to program into my code?
mysql
Share Follow edited May 12, 2018 at 12:08 asked Jan 28, 2011 at 13:01
medium
4,176 16 55 67
3 You don't need to care about these. Modern day's Databases are smart enough in
concurrency control :) – Tauquir Jan 28, 2011 at 13:09
3 thats what I figured I just wanted to double check – medium Jan 28, 2011 at 13:13
Apart from wrapping queries in transactions, you only need to handle deadlocks, which
don't occur very often. – Carlos López Marí May 7, 2019 at 9:49
SQL statements are atomic. That is, if you execute something like this:
Between these queries, another user can change the table Cars and update Sold. To
prevent this, wrap it in a transaction:
BEGIN;
a = SELECT Sold FROM Cars;
UPDATE Cars SET Sold = a + 1;
COMMIT;
is this statement true? Nobody can change the Sold variable during this statement. It is
always incremented by 1, even if somebody else is executing the same statement concurrently
– nulll Jan 7, 2021 at 11:04
1 @nulll Yes, it's true, the it's A in ACID, Atomicity. – doug65536 Nov 17, 2021 at 23:47
What about the following concurrent queries? UPDATE jobs SET assigned_to = 1 WHERE
assigned IS NULL LIMIT 1; UPDATE jobs SET assigned = 2 WHERE ts IS NULL AND
assigned_to IS NULL LIMIT 1; They will always "assign" two rows? Or I can have a situation
where only one row is assigned (to 1 or 2). Trying to make forever running concurrent
scripts pulling jobs to do with SELECT * FROM jobs WHERE assigned = <script_id> (1 or 2),
exit if empty, obviously without premature terminating scripts if there are jobs to do.
– Marco Marsala Dec 14, 2022 at 9:39
Join Stack Overflow to find the best answer to your technical question, help others answer
theirs.