Applying updates is a two-phase process that should occur in the context of a database component's transaction so that your application can recover gracefully from errors. For information about transaction handling with database components, see Managing Transactions.
When applying updates under database transaction control, the following events take place:
- A database transaction starts.
- Cached updates are written to the database (phase 1). If you provide it, an OnUpdateRecord event is triggered once for each record written to the database. If an error occurs when a record is applied to the database, the OnUpdateError event is triggered if you provide one.
- The transaction is committed if writes are successful or rolled back if they are not,
Write Status |
Transaction |
Successful |
Database changes are committed, ending the database transaction. Cached updates are committed, clearing the internal cache buffer (phase 2). |
Unsuccessful |
Database changes are rolled back, ending the database transaction. Cached updates are not committed, remaining intact in the internal cache. |
For information about creating and using an OnUpdateRecord event handler, see Creating an OnUpdateRecord Event Handler. For information about handling update errors that occur when applying cached updates, see Handling Cached Update errors.
Note: Applying cached updates is particularly tricky when you are working with multiple datasets linked in a master/detail relationship because the order in which you apply updates to each dataset is significant. Usually, you must update master tables before detail tables, except when handling deleted records, where this order must be reversed. Because of this difficulty, it is strongly recommended that you use client datasets when caching updates in a master/detail form. Client datasets automatically handle all ordering issues with master/detail relationships.
There are two ways to apply BDE-based updates:
- You can apply updates using a database component by calling its ApplyUpdates method. This method is the simplest approach, because the database handles all details of managing a transaction for the update process and of clearing the dataset's cache when updating is complete.
- You can apply updates for a single dataset by calling the dataset's ApplyUpdates and CommitUpdates methods. When applying updates at the dataset level you must explicitly code the transaction that wraps the update process as well as explicitly call CommitUpdates to commit updates from the cache.
Warning: To apply updates from a stored procedure or an SQL query that does not return a live result set, you must use
TUpdateSQL to specify how to perform updates. For updates to joins (queries involving two or more tables), you must provide one
TUpdateSQL object for each table involved, and you must use the
OnUpdateRecord event handler to invoke these objects to perform the updates. See
Using update objects to update a dataset for details.