简体   繁体   中英

How to UPDATE the same mysql row from multiple scripts at the same time?

I am forking some php scripts and I need to update a mysql row to track some progress. I read that I should use InnoDB for this but I coulnd't find any complete example I can understand. A friend told me he uses this php code:

mysql_query("SET AUTOCOMMIT=0;");
mysql_query("START TRANSACTION;");
mysql_query("SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;");

// UPDATE QUERY HERE

mysql_query("COMMIT;");
mysql_query("UNLOCK TABLES;");      
mysql_query("SET AUTOCOMMIT=1;");

He told me he tried other methods but everything else than that code overloaded his server after some time.

Does someone could tell me if this code will do what I need or if there's a better way to do it?

The database type doesn't matter. MySQL is designed to automatically queue the asynchronous requests and execute them in the order in which they arrive. However, the requests will probably not even be asynchronous, unless you have a lot of them running at once. Either way, you shouldn't have a problem with this, unless your scripts have to update the row in a specific order. In that case, you should use a single script where you can more closely control the updating order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM