FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour ADO record locking experiences
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
ADO record locking experiences
Posted: Fri Aug 22, 2014 04:23 AM

I would like to start a thread to discuss the way you manage record locking using ADO.

My question is simple:

Lets say that you have a record, and that you need to edit it and meanwhile you edit it, other users should not be able to edit it.

How are you managing this situation ?

I appreciate your comments

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: ADO record locking experiences
Posted: Fri Aug 22, 2014 06:42 AM

Antonio,

I have created a table 'FILELOCK' in my SQL table, with a field 'table','table_id','user','pcname'.
When I want to lock a record, I write te table,record-ID, user and PC-name to that table. (I created a function SQLLock)

An other user that want to open the record check with a function SQLlock that the record is available.
If not, it returns FALSE, otherwhise is locks the record by writing the info to the table FILELOCK.

I use the info USER and PCNAME, so I can display the purson who have locked the record...

So my function SQLlock works the same as rlock()

If I want to unlock the record, I delete the record in FILELOCK.

Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: ADO record locking experiences
Posted: Fri Aug 22, 2014 01:09 PM

Antonio

I use a numeric signature file in the table that gets incremented +1 each time I save a record... user one fetches a record that has a current value ( lets say ) 1 and before I UpDate() that record, I have a function that opens a new recordset just on the rows primary key ( or rowid ) and the Signature field to check its value.

If the Value is the same, I know no one has modified the record before I did. If the value has changed, then I know my edit has become 'stale' and someone has modified the same record before I did, in which case I cancel the Edit with a message like "Sorry .. someone has modified the record you are trying to change"....

Rick Lipkin

ps .. this assumes you buffer all your fields to memory values when you first read a record.

Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: ADO record locking experiences
Posted: Fri Aug 22, 2014 03:02 PM

Marc, Rick,

And how do you control if a user gets disconnected without releasing the locked record (using any of your techniques) ?

In that situation the record would appear as locked but in fact nobody is locking it anymore...

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 2706
Joined: Fri Oct 07, 2005 01:50 PM
Re: ADO record locking experiences
Posted: Fri Aug 22, 2014 05:45 PM

Antonio

I do not look for a lock .. whom ever gets to update the record first gets the Signature field+1 Update(). Then if workstation two tries to update the same record .. Within the Update() routine, I create a new recordset on the unique rowid ( primary key ) and see if the signature field matches .. if it doesn't .. I stop workstation 2 from Update() forcing them to cancel their edit and try again.

In the case of a disconnect .. that record never got it signature field updated+1 so when workstation 2 tries to update .. the signature field matches and WS two gets the update.

If WS 1 comes back online .. more than likely they would have had to re-boot and would have to go back into the program afterward WS 2 made their changes.

Hope you understand my logic ?

Rick Lipkin

Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: ADO record locking experiences
Posted: Fri Aug 22, 2014 05:56 PM
Antonio Linares wrote:Marc, Rick,

And how do you control if a user gets disconnected without releasing the locked record (using any of your techniques) ?

In that situation the record would appear as locked but in fact nobody is locking it anymore...


Antonio,

If someone start the program, the program removes first all locks of that PC.
The records are indeed locked if the program have crashed. After restarting the program, all locks are removed.
At the moment, I'm modifiying my function, so it look for all users that are connected with the database.
The Recordset-command that I use now is
Code (fw): Select all Collapse
SELECT SUBSTRING_INDEX(host, ':', 1) AS host_short FROM   information_schema.processlist WHERE db = "MyProgram" GROUP  BY host_short ORDER  BY host_short


Then I get a list af all connections.
I will let you know the result...
Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 1283
Joined: Fri Feb 10, 2006 02:34 PM
Re: ADO record locking experiences
Posted: Fri Aug 22, 2014 06:17 PM

Marc,

But if u dont reconect then register to continue lock, or if u reconnect with another pc its the same...

Good thread

Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
HIX -> https://github.com/carles9000/hix
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: ADO record locking experiences
Posted: Fri Aug 22, 2014 06:23 PM
Carles wrote:Marc,

But if u dont reconect then register to continue lock, or if u reconnect with another pc its the same...

Good thread


Charles,

That's the reason why I'm modifying my function.
If I want to lock a record that is locked by someone else, I will run the query that check if the user is still connected, and then delete the records if it is disconnected..
Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 1283
Joined: Fri Feb 10, 2006 02:34 PM
Re: ADO record locking experiences
Posted: Fri Aug 22, 2014 06:35 PM

Marc,

Now i'm undertsand the process. Exist this table in all rdbm ?

Salutacions, saludos, regards

"...programar es fácil, hacer programas es difícil..."

UT Page -> https://carles9000.github.io/
Forum UT -> https://discord.gg/bq8a9yGMWh
HIX -> https://github.com/carles9000/hix
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: ADO record locking experiences
Posted: Sat Aug 23, 2014 03:31 PM
Antonio,

Antonio Linares wrote:I would like to start a thread to discuss the way you manage record locking using ADO.

My question is simple:

Lets say that you have a record, and that you need to edit it and meanwhile you edit it, other users should not be able to edit it.

How are you managing this situation ?

I appreciate your comments


You have to use pessimistic locking and error trapping. This is the way ADO works.

EMG
Posts: 44162
Joined: Thu Oct 06, 2005 05:47 PM
Re: ADO record locking experiences
Posted: Sat Aug 23, 2014 06:42 PM

Enrico,

Could you post an example ? thanks

regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 9022
Joined: Thu Oct 06, 2005 08:17 PM
Re: ADO record locking experiences
Posted: Sat Aug 23, 2014 07:15 PM
Antonio,

please look at LockType property:

http://www.w3schools.com/ado/prop_rs_locktype.asp

If you set it to adLockPessimistic, the second user trying to assign a value to the record, before the first one have called update method, will get a runtime error. With adLockOptimistic the lock is only on update call. You can use one of the two modes. Or you can lost yourself among the many DIY solutions. :-)

EMG

Continue the discussion