Forum Moderators: phranque

Message Too Old, No Replies

Locking MySQL Tables

Necessary or not?

         

Batman

4:09 am on Mar 6, 2003 (gmt 0)

10+ Year Member



I'm currently working on my first PHP script that uses MySQL as a database. I'm pretty familiar with PHP but MySQL is relatively new. In none of the PHP/MySQL examples I see on the web and in books, tables are being locked when writing data to them.

Does MySQL do this automatically? If I understand the info on their website correctly, you only need to specifically ask for a lock if you use InnoDB or BDB type tables (whatever those are). Correct?

Thanks!

Birdman

2:35 pm on Mar 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



From the MySql manual, LOCK_TABLES [mysql.com] section:


If you are using a storage engine in MySQL that doesn't support transactions, you must use LOCK TABLES if you want to ensure that no other thread comes between a SELECT and an UPDATE. The example shown here requires LOCK TABLES in order to execute safely:

mysql> LOCK TABLES trans READ, customer WRITE;
mysql> SELECT SUM(value) FROM trans WHERE customer_id=some_id;
mysql> UPDATE customer SET total_value=sum_from_previous_statement
-> WHERE customer_id=some_id;
mysql> UNLOCK TABLES;]

There is some other good info on that page.

Batman

2:37 pm on Mar 8, 2003 (gmt 0)

10+ Year Member



OK, I'll take a closer look at it. Thanks!