Database Problem

Database Problem

Postby Colin Haig » 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: Select all  Expand view

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
Colin Haig
 
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Re: Database Problem

Postby James Bott » Thu Oct 14, 2010 2: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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Database Problem

Postby Colin Haig » Thu Oct 14, 2010 7: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
Colin Haig
 
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Re: Database Problem

Postby James Bott » Thu Oct 14, 2010 1: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: Select all  Expand view
           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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Database Problem

Postby Colin Haig » 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: Select all  Expand view

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
Colin Haig
 
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Re: Database Problem

Postby James Bott » 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: Select all  Expand view
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: Select all  Expand view
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
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 84 guests