You can use a semaphore architecture type. For example, create a table called "semaphore" or whatever you want to name. Let's say that there is only one field in the table, and this field is unique and is called "sem". Now run "INSERT INTO semaphore SET sem = 42";
It is good that the INSERT operator is atomic and will mean that at the moment when someone else is trying to insert 42, they will be given an error indicating the duplicate key.
Then do inserts in the source table. Do it all inside the transaction. In SQL, it will look like this:
BEGIN TRANSACTION; INSERT INTO semaphore SET sem = 42; INSERT INTO t1 set uid=42, foo=1; INSERT INTO t1 set uid=42, foo=2; COMMIT; DELETE FROM semaphore WHERE sem = 42;
The reason you delete it later is twice:
- I think you do not need to delete it, but release it for clean data, we will;)
- The reason you delete after COMMIT, you want to lock the lock until the transaction is completed.
Side note. Semaphores are usually used when the auto-increment field will not work. Then you use a single-entry semaphore table to serialize the inserts and lock the primary key.
Hope this helps :)
Sarel source share