FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Check lock-status of record without locking it.
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Check lock-status of record without locking it.
Posted: Fri Sep 04, 2009 08:14 AM

Hi,

How can I check of a reckord is locked by an other user without locking it (so not use rlock())
I already tested it with DbRecordInfo(2), but this return .f. even when it's locked :(

Marc

Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: Check lock-status of record without locking it.
Posted: Fri Sep 04, 2009 08:44 AM

Hi,

I also tested it with DbRLockList(), but this function returns an empty array, even when someone has locked a record.
Does it only return the records locked by the lockal PC , and not the records locked by someone else?

Marc

Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 1467
Joined: Mon Oct 10, 2005 11:26 AM
Re: Check lock-status of record without locking it.
Posted: Fri Sep 04, 2009 09:26 AM
Hello Mark,

I have written my own locking system which is working very well.

First of all, I have 3 fields in every DBF-file I use :

PRDURE D 20
PRLINE N 5
CTRAFK C 3

To lock my records, I use this code :
Code (fw): Select all Collapse
FUNCTION RecLock(RLWijze,RLProc,RLLine,RLWaitSec,RLTaal,RLShow,RLAltijd,RLUser)

   LOCAL lForever
   LOCAL nWait
   LOCAL lRedo      := .T.
   LOCAL nOrde      := INDEXORD()

   DEFAULT(RLWijze,1)
   DEFAULT(RLWaitSec,0.1)
   DEFAULT(RLTaal,"N")
   DEFAULT(RLShow,.T.)
   DEFAULT(RLAltijd,.F.)

   IF RLWijze = 2
      DEFAULT(RLUser,xLockUser)
   ENDIF

   SET ORDER TO 0

   lForever=(RLWaitSec==0)

   DO WHILE lRedo
      nWait=RLWaitSec*2
      DO WHILE lForever .OR. nWait>0
         IF RLOCK()
            IF !NETERR()
               IF RLWijze = 1
                  SET ORDER TO nOrde
                  RETURN(.T.)
               ELSE
                  IF RLAltijd .OR. EMPTY(&(ALIAS()+"->CTRAFK")) .OR. UPPER(ALLTRIM(&(ALIAS()+"->CTRAFK"))) = UPPER(ALLTRIM(RLUser))
                     &(ALIAS()+"->CTRAFK") := RLUser
                     &(ALIAS()+"->PRDURE") := RLProc
                     &(ALIAS()+"->PRLINE") := RLLine
                     IF xCommit
                        COMMIT
                     ELSE
                        UNLOCK
                        RLOCK()
                     ENDIF
                     SET ORDER TO nOrde
                     RETURN(.T.)
                  ENDIF
               ENDIF
            ENDIF
         ENDIF
         INKEY(.5)
         nWait--
      ENDDO
      IF RLShow
         lRedo := MsgYesNo("De gegevens welke U wenst aan te passen,"+CHR(13)+;
                           "zijn in gebruik met de volgende parameters :"+CHR(13)+;
                            CHR(13)+;
                            "Betreft de volgende gegevens :"+CHR(13)+;
                            CHR(13)+;
                            "  - Bestand : "+ALIAS()+CHR(13)+;
                            "  - Record nr. : "+ALLTRIM(STR(RECNO()))+CHR(13)+;
                            CHR(13)+;
                            "U wilt deze gegevens in gebruik nemen :"+CHR(13)+;
                            CHR(13)+;
                            IF(RLWijze=2,"  - Gebruiker : "+ALLTRIM(RLUser)+CHR(13),"")+;
                            "  - Procedure : "+RLProc+CHR(13)+;
                            "  - Lijn nr. : "+ALLTRIM(STR(RLLine))+CHR(13)+;
                            CHR(13)+;
                            "Deze gegevens worden gebruikt door :"+CHR(13)+;
                            CHR(13)+;
                            IF(EMPTY(&(ALIAS()+"->CTRAFK")),"- Softwarelock","  - Gebruiker : "+ALLTRIM(&(ALIAS()+"->CTRAFK"))+CHR(13)+;
                                                                                                IF(!EMPTY(&(ALIAS()+"->PRDURE")),"  - Procedure : "+ALLTRIM(&(ALIAS()+"->PRDURE"     ))+CHR(13),"")+;
                                                                                                IF(&(ALIAS()+"->PRLINE")<>0     ,"  - Lijn nr. : " +ALLTRIM(STR(&(ALIAS()+"->PRLINE")))+CHR(13),""))+;
                            CHR(13)+;
                            "Wenst U opnieuw te proberen ?",;
                            "Kies "+CHR(34)+"JA"+CHR(34)+" of "+CHR(34)+"NEE"+CHR(34))
      ELSE
         lRedo := .F.
      ENDIF
      IF !lRedo .AND. RLWijze = 2 ; UNLOCK ; ENDIF
   ENDDO

   SET ORDER TO nOrde

RETURN(.F.)


To unlock my record, I use this code :
Code (fw): Select all Collapse
PROCEDURE RecUnLock(RLWijze,RLProc,RLLine,RLUser)

   DEFAULT(RLWijze,1)

   IF RlWijze = 2
      DEFAULT(RLUser,xLockUser)
   ENDIF

   IF RLWijze = 1
      IF xCommit ; COMMIT ; ENDIF
      UNLOCK
   ELSE
      IF !EMPTY(&(ALIAS()+"->CTRAFK")) .AND. UPPER(ALLTRIM(&(ALIAS()+"->CTRAFK"))) = UPPER(ALLTRIM(RLUser))
         IF RecLockJuda(1,PROCNAME(),PROCLINE(),2,TaalCode)
            &(ALIAS()+"->CTRAFK") := SPACE( 3)
            &(ALIAS()+"->PRDURE") := SPACE(20)
            &(ALIAS()+"->PRLINE") := 0
         ENDIF
         IF xCommit ; COMMIT ; ENDIF
         UNLOCK
      ENDIF
   ENDIF

RETURN


To lock my record, all I do is :
Code (fw): Select all Collapse
RecLock(2,PROCNAME(),PROCLINE(),5,TaalCode)

And to unlock :
Code (fw): Select all Collapse
RecUnLock(2,PROCNAME(),PROCLINE())


I always see who has locked a record and where he has locked it.
It's always a very intereting something for debugging.

If any questions, don't hesitate to ask.

Good luck.

Regards,

Michel D.
Genk (Belgium)


_____________________________________________________________________________________________

I use : FiveWin for (x)Harbour v. 25.12 - Harbour 3.2.0 (May 2025) - xHarbour Builder (January 2020) - Bcc773

Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Check lock-status of record without locking it.
Posted: Fri Sep 04, 2009 01:28 PM
Marc,

If you are using TDatabase it has a method isLocked(). If you are not using TDatabase, then use the code in the islocked() method.

I highly recommend using a database class. For more about using database classes see my website at http://www.gointellitech.com

James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: Check lock-status of record without locking it.
Posted: Fri Sep 04, 2009 02:06 PM

Michel,

Thanks for the example. Before, I was using something simular, but there was a problem when for some reason the program ended not normal (error or crash PC). The record was always locked for the other users.
Then the program should be restarted on that PC for unlocking the record. Sometime he user didn' restard the program, with the result that nobody can access that record anymore. I was thinking for a method tho theck that the PC that has locked the record is still running the program, but I did'n find a good solution for that yet. A was thinking for using a timer a always update a record. If the record isn't updated anymore, the program is offline.
How did you handel this?

James,

I will take a look at the TDatabase class.

Regards,
Marc

Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Check lock-status of record without locking it.
Posted: Fri Sep 04, 2009 03:03 PM

Marc,

If you are using pessimistic locking this makes the problem worse. By "pessimistic" locking I mean that the record is locked whenever a user is editing a record. When doing this the record can be locked for a long time and there is more chance that a crash could happen.

If you use "optimistic" locking then you only lock the record when the user saves the changes. The record is only locked for a split second and thus there is less chance of a problem.

James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 1195
Joined: Mon Oct 17, 2005 05:41 AM
Re: Check lock-status of record without locking it.
Posted: Fri Sep 04, 2009 03:21 PM

James,

I agree with you in normal case, but in this program I must use that kind of locking.
The program I'm writing is a truck-planning-program for a transport-company. If one user is modifying a route of a driver, other planners are not allowed to change this. The user is also not allowed to drag the route of one driver to another driver if someone else is changing the route...

Regards,
Marc

Regards,

Marc



FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite

Continue the discussion