Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySql table lock in PHP

I am making a webservice in PHP which does a series of calculations based on a select from a table and then updates the table afterwards with the new results.

However i want to prevent the case where another person is making a call to the same webservice while another person's session is still doing an update.

Is it the right thing here to lock that entire table and then unlock it again? If so, how do i lock and unlock a mysql table using PHP pdo?

like image 867
user1574041 Avatar asked Oct 21 '22 21:10

user1574041


1 Answers

Database Management Systems like MySQL are smart enough to prevent concurrency violations like these.

Look for database isolation levels (read uncommitted, read commited, repeatable read, serializable) and the possible problems (dirty read, non repeatable read ...) -> Wikipedia.

Personally, I would not recommend a table lock in your case. You better wrap your calculations and database operations in a transaction and rely on the DBMS to manage your stuff.

like image 197
David Müller Avatar answered Oct 24 '22 11:10

David Müller