Management of Conflicts
Whether you choose buffering, transactions, or views, you must manage conflicts during the update process.
Managing Buffering Conflicts
You can make data update operations more efficient by carefully choosing how and when to open, buffer, and lock data in a multiuser environment. You should limit the time a record or table is subject to access conflicts. Still, you must anticipate and manage the inevitable conflicts that result. A conflict occurs when one user tries to lock a record or table that's currently locked by another user. Two users cannot lock the same record or table at the same time.
Your application should contain a routine to manage these conflicts. If your application doesn't have a conflict routine, the system can lock up. A deadlock occurs when one user has locked a record or a table and tries to lock another record that's locked by a second user who, in turn, is trying to lock the record that's locked by the first user. While such occurrences are rare, the longer that a record or table is locked, the greater the chance of deadlock.
Trapping Errors
Designing a multiuser application or adding network support to a single-user system requires that you deal with collisions and trap for errors. Using Visual FoxPro record and table buffers simplifies some of this work.
If you attempt to lock a record or table already locked by another user, Visual FoxPro returns an error message. You can use SET REPROCESS to automatically deal with unsuccessful lock attempts. This command, in combination with an ON ERROR routine and the RETRY command, enables you to continue or cancel the lock attempts.
The following example demonstrates automatic reprocessing of a failed operation, using SET REPROCESS.
Using SET REPROCESS and ON ERROR to Manage User Collisions
Code | Comment |
---|---|
|
This routine runs if an error occurs. Open the files non-exclusively. Reprocessing of unsuccessful locks is automatic. Open the table. Create the APPEND FROM table if needed. |
|
The main routine starts here. These commands are examples of codes that could be executed in the course of your program. |
|
The main routine ends here. |
|
Routine to append a blank record. |
|
Routine to replace data in the current record. |
|
Routine to replace data in all records. |
|
Routine to replace data in the current record. |
|
Routine to append records from another file. |
The following example demonstrates an error procedure that starts when the user presses ESC.
Error Handling Using the ESC Key
Code | Comment |
---|---|
|
This program is called when an error is encountered and the user escapes from the wait process. |
|
Figure out what kind of error this is. Is it "File is in use by another"? |
|
|
|
Or "Record is in use by another"? |
|
Or is it unknown? |
|
Display the error message in a dialog box with an exclamation point and an OK button. |
See Also
Management of Updates with Views | Detecting and Resolving Conflicts | Programming for Shared Access | SET REPROCESS | ON ERROR | RETRY