Domanda

Ok, look at this scenario, I got a table in which users may modify many rows at the same time. I want that when someone is modifying records then other people can't modify the same record. Ex, there's table1 with 3 columns (ID, text, flag):

ID-text-flag
11-txt1-0
12-txt2-0
13-txt3-0
14-txt4-0

I did some research about Concurrency Control in Mysql, the 1st solution that people suggest is to use SELECT ... FOR UPDATE or SELECT ... LOCK IN SHARE MODEin Mysql. However, there is a limitation for this solution, that is u have to update immediately right after you select a record. For example:

SELECT text FROM table1 where ID<=20 FOR UPDATE;
UPDATE table1 SET text = 'new text' where ID<=20;

However, my application requires users to download the data to the Gui & then the users may spend many hours to work on that data before they commit to update.

There is a second solution, Mysql provide the Row Lock mechanism, but this solution require to use InnoDB & its overhead is quite high.

Another option is to use table lock in mysql, the overhead is quite low, but we can't lock the whole table for many hours right? For example, user A may modify the record 1 to 10, user B at the same time may need to modify record 11 to 20, so the user B shouldn't wait until the user A finishes modifying.

In my opinion, i want to have a column "flag", when user A is modifying some records, the flag of these records will turn to 1, if user B wants to modify the same records, the system will popup message saying someone is modifying it. When user A finishes, it will bring the flag to 0, & user A can modify these same record.

But there is a complicated problem for this solution, what if user A forgot to save data? if that happens then the flag is 1 forever? Then the flag should have an expired time after a few hours? & how to do that?

Maybe we need time-stamp or some mechanism to let the flag turn back to 0 after a certain time? But this is more complicated than i thought. We can't let the DB to check the time-stamp of the flag every 1 hour?

--> I am not sure if this is the most elegant solution? I have no commercial experience in DB design & i want to know how DB people manage this issue in a commercial environment?

Can you find a better & more elegant solution than all the above solutions?

È stato utile?

Soluzione

As Zerkms suggested, we do not need flag at all, but we need to build the "Record comparing system" in our Application.

Ok, say any user can download data for updating, after he modifies record & before he actually updates DB, the Record comparing system will check the ID & the old text of the data (not the new data that users has just modified) with the same ID & current text of the data in DB, if they are the same then he can commit updating, but if they are different then it means someone else updated the records before him, so the system will not allow him to update. He then has to download data again & start over.

Since it is very rare that 2 users update the same records at the same time, so i think this solution is quite feasible & easy to implement & do not increase the overhead. This is called the Optimistic concurrency control as we build the Record comparing system at the application level, so no database overhead involved. I think this is the most elegant solution for my application need.

Do u think so?

Note: for this system, we can use auto increment ID (for in the case of inserting new records)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top