Check lock-status of record without locking it.

Check lock-status of record without locking it.

Postby Marc Vanzegbroeck » Fri Sep 04, 2009 8: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
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Check lock-status of record without locking it.

Postby Marc Vanzegbroeck » Fri Sep 04, 2009 8: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
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Check lock-status of record without locking it.

Postby driessen » Fri Sep 04, 2009 9: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: Select all  Expand view
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: Select all  Expand view

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: Select all  Expand view
RecLock(2,PROCNAME(),PROCLINE(),5,TaalCode)

And to unlock :
Code: Select all  Expand view
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. 24.07 - Harbour 3.2.0 (February 2024) - xHarbour Builder (January 2020) - Bcc773
User avatar
driessen
 
Posts: 1422
Joined: Mon Oct 10, 2005 11:26 am
Location: Genk, Belgium

Re: Check lock-status of record without locking it.

Postby James Bott » Fri Sep 04, 2009 1: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Check lock-status of record without locking it.

Postby Marc Vanzegbroeck » Fri Sep 04, 2009 2: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
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Check lock-status of record without locking it.

Postby James Bott » Fri Sep 04, 2009 3: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Check lock-status of record without locking it.

Postby Marc Vanzegbroeck » Fri Sep 04, 2009 3: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
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 89 guests