0% found this document useful (0 votes)
41 views2 pages

MySQL Concurrency, How Does It Work and Do I Need To Handle It in My Application - Stack Overflow

The document discusses MySQL concurrency, specifically with the InnoDB table engine, and addresses concerns about simultaneous updates or inserts by multiple users. It explains that MySQL handles concurrency through atomic SQL statements and suggests using transactions to manage dependent queries. Additionally, it highlights the importance of handling deadlocks and the need for appropriate isolation levels in transactions.

Uploaded by

agcolab92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

MySQL Concurrency, How Does It Work and Do I Need To Handle It in My Application - Stack Overflow

The document discusses MySQL concurrency, specifically with the InnoDB table engine, and addresses concerns about simultaneous updates or inserts by multiple users. It explains that MySQL handles concurrency through atomic SQL statements and suggests using transactions to manage dependent queries. Additionally, it highlights the importance of handling deadlocks and the need for appropriate isolation levels in transactions.

Uploaded by

agcolab92
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

MySQL concurrency, how does it work and do I need

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?

If I do have to program it into my code how do you go about handling a


concurrency case like this?

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

1 Answer Sorted by: Highest score (default)

SQL statements are atomic. That is, if you execute something like this:

40 UPDATE Cars SET Sold = Sold + 1


Join Stack Overflow to find the best answer to your technical question, help others answer
theirs. Nobody can change the Sold variable during this statement. It is always
incremented by 1, even if somebody else is executing the same statement
ign up with email Sign up with Google Sign up with GitHub Sign up with Facebook
concurrently.
The problem occurs if you have statements that depend on each other:

a = SELECT Sold FROM Cars;


UPDATE Cars SET Sold = a + 1;

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;

Transactions are supported by InnoDB, but not by MyISAM.

Share Follow answered Jan 28, 2011 at 13:10


Sjoerd
74.5k 16 134 175

21 Even in a transaction there's no guarantee. MySQL by default, does row-level locking/table-


locking only for write operations, even in transactions. To make a have a correct value, it's
necessary to put FOR UPDATE in SELECT, or to specify a higher isolation level on
TRANSACTION as SERIALIZABLE. More info: Internal locking, Isolation levels, Locking Reads
– Alan CN Jan 5, 2017 at 18:51

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.

ign up with email

You might also like