Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Can I save data in different dbfs in Multi user Envrn. 1

Status
Not open for further replies.

rskomal

Programmer
Feb 2, 2003
13
0
0
IN
I am trying to develop an application under Multi user Environment. I have to save information in two different databases. If I lock first dbf i think the second may being used by other user or vise versa. How can I safely place data in both dbfs, Please suggest. Thanks
 
If you do not want anyone to access the dbf's use flock() on both files.

If you only want to lock the new updated or inserted record in both dbf's use flock()/lock()

To unlock the files and or records use unlock.

Rob.
 
To test if you have both files locked to start with use:

if dbf1->flock() .and. dbf2->flock()
** do whatever you want to do
** and unlock the files
unlock all
else
? "Unable to lock both dbf's"
endif

Rob.

 
Dear Rob thanks a lot But Is it possible to lock more than one file with f/rlock. I think locking is avvailable for one file at a time.
 
Yes, we do something similar... kind along the following

DB2->(xFLock())
DB3->(xFLock())
DB4->(xFLock())

... manipulate data

xUnLockAll()

Procedure xFLock()
Local cScreen := SaveScreen()

Do While ! FLock()
@15,0 Say PadC("Waiting To Lock File In Use" + ;
" By Another User",80) Color "R+*/N
InKey(.2)
EndDo

RestScreen(,,,,cScreen)

Return

Richard L. Hankins Jr.
Senior Programmer
Auction Services, Inc.
 
I have tried both the techniques but data lost while I test the program on two computers, I used the following simple coding:

xLoops = .t.

DO WHIL xLOOPs

@x,y say "Bill No " get mBill_No
@x,y say "Date " get mBill_Date
@x,y say "Party Name " get m_Cust_name
@x,y say "Item Name " get mItem_Name
@x,y say "Quantity " get m_qty
@x,y say "Total Amt. " get m_amount
Read

If Lastkey() = K_ESC
@23,1 say "Save, Cancel..."
Do while !chr(inkey(0))$"SsCc"+chr(K_ESC)+chr(K_RETURN)
Enddo

DO CASE
Case chr(lastkey())$"Ss" + chr(K_ENTER)

if bills->(Flock()) .and. data->(Flock())
Save_Data_Function()
Else
Retry procedure ...
endif

OtherWise
xLoops = .f.
ENDCASE

EndIf

ENDDO


Function Save_Data_Function()
*---------------------------*
Sele Bills
Appen Blan
Replace Bill_no with mBill_no, Bill_date with mBill_date

Sele Data
appen Blan
Replace Cust_name with mCust_Name, Quantity with m_Qty, ;
Amount with m_Amount

UnLock All
Return .t.

Just let me know what's wrong with these lines.

Thanks

 
Once I changed your save function to this it worked for me... the rest did not compile for me... I had to rig up some FAUX databases and stuff to test...


Function Save_Data_Function()
** Loop Until We Append A New Record
Do While ! Bills->(DBAppend())
** Let them know whats going on
MESSAGE "Waiting To Append Record To Bills"
EndDo

** If we are at this point then we have appended
** so we can modify the data at this point
Bills->Bill_No := mBill_No
Bills->Bill_Date := mBill_Date

** Loop till we append
Do While ! Data->(DBAppend())
** Keep Them Aware We Are Working
MESSAGE "Waiting To Append Record To Data"
EndDo

** Update the information in the new record
Data->Cust_Name := mCust_Name
Data->Quantity := m_Qty
Data->Amount := m_Amount

** Commit all changes and unlock all files
DBCommitAll()

Return .t.

Richard L. Hankins Jr.
Senior Programmer
Auction Services, Inc.
 
Oops I forgot I changesd the following also

if bills->(Flock()) .and. data->(Flock())
Save_Data_Function()
Else
Retry procedure ...
endif

to just Save_Data_Function since we don't need it if we are looping for the append

Richard L. Hankins Jr.
Senior Programmer
Auction Services, Inc.
 
Just a thought, why are you locking the files when adding records, this is not very multiuser friendly.

I would think you would only need something like this in your Save Data:

Function Save_Data_Function()
*---------------------------*
Sele Bills
while ! AddRec() && keeps retrying until one is added
end
Replace Bill_no with mBill_no, Bill_date with mBill_date

Sele Data
while ! AddRec()
end
Replace Cust_name with mCust_Name, Quantity with m_Qty, ;
Amount with m_Amount

UnLock All
Return .t.



****
* ADDREC function
*
* Returns true if record appended. The new record is
* current and locked.
* Pass the following parameter
* 1. Numeric - seconds to wait
*

FUNCTION ADDREC(nWAIT)

if nWAIT == NIL
nWait := 5
endif

APPEND BLANK
IF .NOT. NETERR()
RETURN (.T.)
ENDIF

WHILE nWAIT >= 0

APPEND BLANK
IF .NOT. NETERR()
RETURN .T.
ENDIF

INKEY(.5) && wait 1/2 second
nWAIT -= .5

ENDDO
Alert('Cannot add a record! '+alias())
RETURN (.F.) && not locked
 
Look at and use the Locks.prg that comes with Clipper. This has a:
RecLock(nSecWait) to lock a record
AddRec(nWaitSec) to add a record
NetUse(cDatabase,lOpenMode,nSeconds) to open databases

All the functions return a logical .T. if successful and .F. if they fail.

Always issue a dbcommit() on the database area after writing data and don't forget to dbunlock() for other users.

Always alias ( such as parts->(eof()) rather than selecting areas) wherever possible as it makes things much easier to read at a later date.

Ian Boys
DTE Systems Ltd
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top