I have an application on sae, but I need to lock the table during user registration, but sae does not have the permission to lock the table for the application, so I want to implement a lock to manage user registration to prevent errors. However, the temporary directory of sae is not shared among requests and the code space is read-only... I have an application on sae, but I need to lock the table during user registration, but sae does not have the permission to lock the table for the application, therefore, I want to implement a lock to manage user registration to prevent errors. However, the temporary directory of sae is not shared among requests and the code space is read-only, in this case, how can we implement the lock?
Reply content:
I have an application on sae, but I need to lock the table during user registration, but sae does not have the permission to lock the table for the application, therefore, I want to implement a lock to manage user registration to prevent errors. However, the temporary directory of sae is not shared among requests and the code space is read-only, in this case, how can we implement the lock?
Why should we lock the table? The final goal is "user registration to prevent errors", which can be done directly using transactions.
------ What if there is no transaction? SAE does not provide InnoDB and can only use MyISAM. You can use the uniqueness of the primary key to create a lock:
- Create
locked_tablesTable to save the locked Table, as follows:
CREATE TABLE `locked_tables` ( `table_id` int(11) NOT NULL, PRIMARY KEY (`table`)) ENGINE=MyISAM DEFAULT CHARSET=latin1
Wheretable_idIs the table ID, which is allocated by itself, suchtestThis table is assigned1.
- Lock table, use
insertInsert the ID of the table to be locked, such as the lock.testTable:
Insert into 'locked _ tables '('table _ id') VALUES (1); -- the id previously allocated by test is 1.
IfinsertIf the execution is successful, the lock table is successful. OtherwiseDuplicate entry '1' for key 'PRIMARY'If the table is locked, wait for the table to be released.
- Release the lock and use
deleteDelete, for exampletestTable:
DELETE FROM `locked_tables` WHERE `table_id` = 1
The general idea is that the specific code may not have time to do it. Please implement it on your own.
How can I use databases?
It is marked as 1 on a record, and 0 on the end.
When someone registers, check that if it is 0, mark it as 1 to start the registration process. After registration is completed, change it to 0. If the check is 1, read this record cyclically until it is 0.
Of course, if the registration process crashes, no one will be able to register, haha. (However, you can add an update_time parameter to determine the maximum waiting time .)