The buffered mode of the data set (Cached mode) is to buffer the database server's data in client memory and no longer relies on the server. The change data is submitted to the database server one time only when the data needs to be committed to the database server for saving.
The biggest advantage of the data set buffering mode is that it reduces the resource consumption on the database server, even when the network is disconnected, and then commits the database after the network connection. Compared with the traditional 2-tier architecture database software, there is a huge ability to resist network failure. PB, including the vast majority of the 2-tier architecture software, once the network temporarily interrupted, causing the database connection is interrupted, the client all failed to save the changes will be discarded, and in ODAC this problem no longer exists.
It is very simple to turn on the dataset buffering mode, which is to set the Cachedupdates property of the DataSet to True, as follows:
Start Local Cache mode
Tdataset.cachedupdates: = True;
When you turn on buffered mode for a dataset, you need code dominance to do the following:
1. See if there is data movement in the dataset, indicating data changes when the Updatespending property of the DataSet is true.
2. View the change state of the current record of the dataset, read the UpdateStatus property of the DataSet,
3. Save the data in the data set buffer, call the method Applyupdates, before calling the method, requires the code to explicitly call the database connection component's transaction opening function, and commit the transaction after the commit succeeds, or rollback the transaction after the commit fails.
4. When Applyupdates succeeds, it is also necessary to call the method Commitupdates clear the buffer. This is different from TClientDataSet and requires manual removal of the buffer.
5. When applyupdates fails, it is necessary to call method Restoreupdates to recover the data status flag that has been submitted so that it can be submitted correctly the next time.
6. Use the Onupdaterecord event to manually process the data that needs to be updated, and once the code for the event is filled in, ODAC will no longer automatically process the record, unlike Tdatasetprovider event handling.
7. Use the Onupdateerror event to handle errors in the update process.
The typical code is as follows:
procedure Applybuttonclick (Sender:tobject);
Begin
with Myquery Do
begin
Session.starttransaction; //Start database transaction
Try
Applyupdates; //Modify data Submission Database
Session.commit; //Submit database transaction without error
Commitupdates; //Clear buffer after successful commit
except
Restoreupdates; //Commit failed, recovery buffer data Update flag
Session.rollback; //Roll Back database transactions
raise; //re-trigger exception, display error message
End;
End;
End;
ODAC (V9.5.15) Learning Note (18) data set buffering mode