Harmeet Singh
Harmeet Singh

Reputation: 1

MySQL Table Lock

Do I need to explicitly lock mysql table for the following case:

mysql database user name : db_user (and is only one)
web client sessions : many users logged in simultaneously

As the mysql user is single, but I have many client sessions with my web server. what will happen (INSERT RECORD IN ONE TABLE THEN GET INSERT ID AND ADD DATA TO ANOTHER TABLE):

  1. many web sessions have separate copy of db_user.
  2. a single copy of db_user for many browser sessions.
  3. any other.

Please help.

Upvotes: 0

Views: 5364

Answers (1)

Ross Smith II
Ross Smith II

Reputation: 12179

Harmeet,

The short answer is no, you do not need to use LOCK TABLES when doing what you describe.

You only need to use LOCK TABLES when you want to be assured that you are the only one writing to a table, for example, if you want to INSERT a large number of records, and want the operation to occur as fast as possible.

You can see this in the dump files created by the mysqldump command. Here's an example from a dump file:

LOCK TABLES `tbl` WRITE;
/*!40000 ALTER TABLE `tbl` DISABLE KEYS */;
INSERT INTO `tbl` VALUES ...
/*!40000 ALTER TABLE `tbl` ENABLE KEYS */;
UNLOCK TABLES;

Upvotes: 4

Related Questions