Recordset: Locking Records (ODBC)
This article applies to the MFC ODBC classes. For DAO recordsets, see the article DAO Recordset.
This article explains:
When you use a recordset to update a record on the data source, your application can lock the record so no other user can update the record at the same time. The state of a record updated by two users at "the same time" is undefined unless the system can guarantee that two users cannot update a record simultaneously.
Note This article applies to objects derived from CRecordset in which bulk row fetching has not been implemented. If you have implemented bulk row fetching, some of the information does not apply. For example, you cannot call the Edit and Update member functions. For more information about bulk row fetching, see the article Recordset: Fetching Records in Bulk (ODBC).
The database classes provide two record-locking modes:
- Optimistic locking (the default)
- Pessimistic locking
Updating a record occurs in three steps:
- You begin the operation by calling the Edit member function.
- You change the appropriate fields of the current record.
- You end the operation — and normally commit the update — by calling the Update member function.
Optimistic locking locks the record on the data source only during the Update call. If you use optimistic locking in a multiuser environment, the application should handle an Update failure condition. Pessimistic locking locks the record as soon as you call Edit and doesn't release it until you call Update (failures are indicated via the CDBException mechanism, not by a value of FALSE returned by Update). Pessimistic locking has a potential performance penalty for other users, since concurrent access to the same record may have to wait until completion of your application's Update process.
If you want to change a recordset object's locking mode from the default, you must change the mode before you call Edit.
- Call the SetLockingMode member function, specifying either CRecordset::pessimistic or CRecordset::optimistic.
The new locking mode remains in effect until you change it again or the recordset is closed.
Note Relatively few ODBC drivers currently support pessimistic locking.