How to implement pessimistic locking in a php/mysql web application?

戏子无情 提交于 2020-01-13 09:28:29

问题


How to implement pessimistic locking in a php/mysql web application?

  1. web-user opens a page to edit one dataset (row)
  2. web-user clicks on the button "lock", so other users are able to read but not to write this dataset
  3. web-user makes some modifications (takes maybe 1 to 30 minutes)
  4. web-user clicks "save" or "cancel" and the "lock" is removed

Are there standard methods in php/mysql for this scenario? What happens if the web-user never clicks on "save"/"cancel" but closes the internet-exploror?


回答1:


Traditionally this is done with a boolean locked column on the record in the database that is flagged appropriately.

It is a function of this sort of locking that the lock has to be released, and circumstances may prevent this happening naturally (system crashes, user stupidity, dropped network packets, etc etc etc). This is why you would need to provide some manual unlock method and/or impose a time limit (maybe with a cron job?) on how long a record can be locked for. You could implement some kind of AJAX poll to keep the record locked if the browser is still open? At any rate, you would probably be best to verify the data in the record is the same as it was when the lock was aquired before you modify it.

This limitation of this type of behaviour is particularly prevalent in web applications, but is true of anything that uses this approach - Sage Line 50, for one, is a bugger for it, I regularly have to delete lock files after machine/application crashes.




回答2:


You need to implement a LOCKDATE and LOCKWHO field in your table. Ive done that in many applications outside of PHP/Mysql and it's always the same way.

The lock is terminated when the TTL has passed, so you could do a substraction of dates using NOW and LOCKDATE to see if the object has been locked for more than 30 minutes or 1h as you wish.

Another factor is to consider if the current user is the one locking the object. So thats why you also need a LOCKWHO. This can be a user_id from your database, a session_id from PHP. But keep it to something that identifies a user, an ipaddress is not a good way to do it.

Finaly, always think of a mass-unlock feature that simply resets all LOCKDATEs and LOCKWHOs...

Cheers




回答3:


I would write the locks in one centralized table instead of adding fields to all tables.

Example table structure :

tblLocks

  • TableName (The name of tha locked table)
  • RowID (Primary key of locked table row)
  • LockDateTime (When the row was locked)
  • LockUser (Who locked the row)

With this approach you can find all locks that are made by a user without having to scan all tables. You could kill all locks when user logs out for example.



来源:https://stackoverflow.com/questions/8170611/how-to-implement-pessimistic-locking-in-a-php-mysql-web-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!