Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL concurrency, how does it work and do I need to handle it in my application

Tags:

mysql

I am currently running a MySQL database. All of my tables are using the Table Engine InnoDB.

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?

like image 679
medium Avatar asked Jan 28 '11 13:01

medium


People also ask

How does MySQL deal with concurrency?

Multiversion Concurrency Control. Most of MySQL's transactional storage engines, such as InnoDB, Falcon, and PBXT, don't use a simple row-locking mechanism. Instead, they use row-level locking in conjunction with a technique for increasing concurrency known as multiversion concurrency control (MVCC).

Can MySQL handle concurrent requests?

You may use one buffer table (Temporary table) in the sense of concurrency control with the help of LOCK'ing mechanism of MySQL. So while one request's to the server on the priority base you can set remains request as in queue or in the sense restrict the table.

How many concurrent connections MySQL can handle?

By default 151 is the maximum permitted number of simultaneous client connections in MySQL 5.5. If you reach the limit of max_connections you will get the “Too many connections” error when you to try to connect to your MySQL server. This means all available connections are in use by other clients.


1 Answers

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

UPDATE Cars SET Sold = Sold + 1 

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.

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.

like image 121
Sjoerd Avatar answered Sep 21 '22 10:09

Sjoerd