I made multi user application in which if two user open the same edit page and at the same time both user enter data in the form there would be conflict data so I need a solution like if one user open particular edit page then all other user block for some time and prompt a message to them like" other user edit the same page...."
问题:
回答1:
Maybe,
you could add two columns in your database 'open' and 'time'
Set 'open' to 'true' or 'false' if someone is editing it. Also set a timestamp to the 'time' column. Replace the timestamp every so many seconds while editing takes place.
If someone else opens it, check the 'open' column and of it's 'true', calculate the time passed from the 'time' column. If it's over a certain time (say 2 or 3 minutes), assume the other user isn't editing anymore and allow this user to edit.
To make things clear,
Option 1:
- a user clicks on edit
- check the 'open' column, if it's 'false', set it to 'true' and add the timestamp to 'time'
- As long as the user is typing in the inputfield, every 10 seconds or so, an AJAX call is made to update the 'time' column with the present timestamp
- user clicks 'save', the 'open' column is set to 'false'. -
Option 2:
- a user clicks on edit
- check the 'open' column, if it's 'true'n check the 'time' column.
- if the difference with the current timestamp is more than, lets say, 2 - 3 minutes, allow editing for this user and update the timestamp.
回答2:
Best and easiest way to do it is to keep a live connection between the website and the editing user after he clicks edit.
So the basic steps :
1)user clicks edit.
2)initiate either ajax or websocket connection with that user to tell the server the file is still being edited
3)if user closes manually or just leaves the website, have some logic to deal with it server side
4)profit.
The continous ajax calls to the server(like a chat applciation) can update a "time_last_edited" column, if that column is, say, more than 10 seconds old, file is safe to edit, else it means someone is still editing it.
回答3:
Give the content a modified time. Before the new content is submitted, check the time is the same as it was before. If it is different, then the content has changed and the user should be informed.
Gets around most edge cases.
回答4:
Have a field in the table such as Page_status and set it as locked if a user opens it for editing. and set it to unlocked if editing is completed. And each time check the status before allowing each users to edit.
If the user closes the page without editing. then have session for each users and set time limit and if the session expires then set the status to unlocked.
回答5:
For this you have to keep flag in table, if suppose you have table name "userdetails" with columns such as name, mobile, email, address so now you add two more column name
inused with datatype TINYINT with default value 0 inuseddatetime with datatype datetime with default value NULL
So as soon as particular user open details for edit update inused column to 1 and inuseddatetime column with that currendt date and time
So wen other user open same detail for edit and if the column has value 1 show them prompt.
In order to again change 1 to 0, when first user click submit button again update inused column to 0 and inuseddatetime column to null
In some case if user do not press submit button and leave page so to avoid such condition you can write cron job on server which will check inuseddatetime column and will update inused column to 0 for column which are ideal from last 10 min or 5 min