FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Database Problem
Posts: 310
Joined: Mon Oct 10, 2005 05:10 AM
Database Problem
Posted: Wed Oct 13, 2010 11:18 PM
Hi All

I am rewriting my software using the database class in Fivewin and when testing
found that records were not unlocked.

Using a MDI environment - open job window enter new job and save ( leave this window open) ,
open an invoice window type in job that was just entered - using a valid clause th chcek that the user
enters a valid job returns an invalid job - but if after my save method I add and unlock it works properly.

eg oJb:save()
oJb:unlock()
Code (fw): Select all Collapse
METHOD Save() CLASS TDataBase

   local n
   local lLocked  := .f.

   if ::lBuffer
      if ! ::Eof() .and. ! ::lReadOnly
         if ::lShared
            if ::IsRecLocked( ::RecNo() ) .or. ( lLocked := ::RecLock( ::RecNo() ) )
               ::SaveBuff()
               if lLocked
                  ::Commit()
                  ::RecUnLock( ::RecNo() )
               else
                  MsgInfo('not locked')  ********
               endif
            else
               if ! Empty( ::bNetError )
                  return Eval( ::bNetError, Self )
               else
                  MsgAlert( "Record in use", "Please, retry" )
               endif
            endif
         else
            ::SaveBuff()
         endif
      endif
   endif

return nil

//----------------------------------------------------------------------------//


I am using FWH 9/12 , xHarbour 1.2.1 Rev 6406

Cheers

Colin
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Database Problem
Posted: Thu Oct 14, 2010 02:40 AM

Colin,

I believe it is working as it is supposed to.

If you are using pessimistic locking (locking before entering or editing and unlocking after saving), then you (the programmer) has to lock the record after saving it.

The save method leaves the record in the lock state that it found it.

If it is locked (pessimistic) when the save() method is called, then is is locked when the save() method is completed. This also allows you to lock and update multiple records at one time (multiple records are locked, then updated, then unlocked).

If you use optimistic locking the record is locked, saved, and immediately unlocked during the save method.

Regards,
James

FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 310
Joined: Mon Oct 10, 2005 05:10 AM
Re: Database Problem
Posted: Thu Oct 14, 2010 07:05 AM

Hi James

I dont lock the record - I only call oJb:save() which I presumed would
do the locking and unlocking. Is the locking scheme able to be programmed - if so
how. ?

Cheers

Colin

Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Database Problem
Posted: Thu Oct 14, 2010 01:57 PM
Colin,

Well the logic looks correct. If it is not locked when save() is called, the record is locked the data saved, and then unlocked.

Code (fw): Select all Collapse
            if ::IsRecLocked( ::RecNo() ) .or. ( lLocked := ::RecLock( ::RecNo() ) )
               ::SaveBuff()
               if lLocked
                  ::Commit()
                  ::RecUnLock( ::RecNo() )


Perhaps you can write a small self-contained example showing the problem.

Regards,
James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
Posts: 310
Joined: Mon Oct 10, 2005 05:10 AM
Re: Database Problem
Posted: Thu Oct 14, 2010 11:17 PM
Hi James

After further tests I found that the record did not lock when the save was after appending a
new record.

Code (fw): Select all Collapse
if lNewJob
   oJb:append()
   oJb:save()           
   ** in save method the .or.  was never reached so lLocked remained false
    ** if ::IsRecLocked( ::RecNo() ) .or. (lLocked := ::RecLock( ::RecNo() ))  
else
   ** the .or. was reached and lLocked set to true  
   oJb:save()
endif


So in the database class I modified the append

//METHOD Append() INLINE ( ::nArea )->( DbAppend() )
METHOD Append() INLINE (( ::nArea )->( DbAppend() ) , ( ::nArea )->( DbUnlock() ))

Every thing seems to work okay now but I will keep testing.

Cheers

Colin
Posts: 4840
Joined: Fri Nov 18, 2005 04:52 PM
Re: Database Problem
Posted: Fri Oct 15, 2010 12:17 AM
Colin,

There are two reasons I would not do what you did.

1) After appending a record you really want to keep it locked until you are done with the save, otherwise it is possible for another user to write data to that record.

2) If you modify the source, then you have to modify it each time a new version comes out.

I would leave the database class untouched and simply change your code to this:

Code (fw): Select all Collapse
if lNewJob
   oJb:append()
   oJb:save()           
   oJb:recUnlock()
else
   oJb:save()
endif


Or you could just use my TData database class and all you have to do is:

Code (fw): Select all Collapse
oJb:save()


It's smart--it automatically handles new records and updates without any programming on your part. Of course, it does a lot more too.

Don't you already have TData?

Regards,
James
http://www.gointellitech.com
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10

Continue the discussion