Applying BDEbased cached updates

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" on page 23-6.

When applying updates under database transaction control, the following events take place:

1 A database transaction starts.

2 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.

3 The transaction is committed if writes are successful or rolled back if they are not: If the database write is successful:

  • Database changes are committed, ending the database transaction.
  • Cached updates are committed, clearing the internal cache buffer (phase 2).

If the database write is 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" on page 26-37. For information about handling update errors that occur when applying cached updates, see "Handling cached update errors" on page 26-38.

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.

Important 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" on page 26-40 for details.

+1 0

Responses

  • nebay
    Does TUpdateSQL component handle database transaction?
    8 years ago

Post a comment