Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I lock read/write to MySQL tables so that I can select and then insert without other programs reading/writing to the database?

I am running many instances of a webcrawler in parallel.

Each crawler selects a domain from a table, inserts that url and a start time into a log table, and then starts crawling the domain.

Other parallel crawlers check the log table to see what domains are already being crawled before selecting their own domain to crawl.

I need to prevent other crawlers from selecting a domain that has just been selected by another crawler but doesn't have a log entry yet. My best guess at how to do this is to lock the database from all other read/writes while one crawler selects a domain and inserts a row in the log table (two queries).

How the heck does one do this? I'm afraid this is terribly complex and relies on many other things. Please help get me started.


This code seems like a good solution (see the error below, however):

INSERT INTO crawlLog (companyId, timeStartCrawling) VALUES (     (         SELECT companies.id FROM companies         LEFT OUTER JOIN crawlLog         ON companies.id = crawlLog.companyId         WHERE crawlLog.companyId IS NULL         LIMIT 1     ),     now() ) 

but I keep getting the following mysql error:

You can't specify target table 'crawlLog' for update in FROM clause 

Is there a way to accomplish the same thing without this problem? I've tried a couple different ways. Including this:

INSERT INTO crawlLog (companyId, timeStartCrawling) VALUES (     (         SELECT id         FROM companies         WHERE id NOT IN (SELECT companyId FROM crawlLog) LIMIT 1     ),     now() ) 
like image 659
T. Brian Jones Avatar asked Jul 08 '11 07:07

T. Brian Jones


People also ask

How does MySQL read locks and write locks?

MySQL Locks: Read LocksIf the session holds the READ lock on a table, they cannot perform a write operation on it. It is because the READ lock can only read data from the table. All other sessions that do not acquire a READ lock are not able to write data into the table without releasing the READ lock.

Does MySQL lock table on INSERT?

MySQL uses table locking (instead of row locking or column locking) on all table types, except InnoDB and BDB tables, to achieve a very high lock speed.

Can we lock database in MySQL?

MySQL allows a client session to acquire a table lock explicitly to cooperate with other sessions to access the table's data. MySQL also allows table locking to prevent it from unauthorized modification into the same table during a specific period.

How can you implement locks in MySQL?

To lock a table using the MySQL LOCK TABLES Statement you need have the TABLE LOCK and SELECT privileges. READ LOCK − If you apply this lock on a table the write operations on it are restricted. i.e., only the sessions that holds the lock can write into this table.


2 Answers

You can lock tables using the MySQL LOCK TABLES command like this:

LOCK TABLES tablename WRITE;  # Do other queries here  UNLOCK TABLES; 

See:

http://dev.mysql.com/doc/refman/5.5/en/lock-tables.html

like image 154
qbert220 Avatar answered Oct 04 '22 12:10

qbert220


Well, table locks are one way to deal with that; but this makes parallel requests impossible. If the table is InnoDB you could force a row lock instead, using SELECT ... FOR UPDATE within a transaction.

BEGIN;  SELECT ... FROM your_table WHERE domainname = ... FOR UPDATE  # do whatever you have to do  COMMIT; 

Please note that you will need an index on domainname (or whatever column you use in the WHERE-clause) for this to work, but this makes sense in general and I assume you will have that anyway.

like image 30
wonk0 Avatar answered Oct 04 '22 11:10

wonk0