UniDAC

TMemDataSet.ApplyUpdates Method

Writes dataset's pending cached updates to a database.

Class

TMemDataSet

Syntax

procedure ApplyUpdates; overload; virtual;

Remarks

Call the ApplyUpdates method to write a dataset's pending cached updates to a database. This method passes cached data to the database, but the changes are not committed to the database if there is an active transaction. An application must explicitly call the database component's Commit method to commit the changes to the database if the write is successful, or call the database's Rollback method to undo the changes if there is an error.

Following a successful write to the database, and following a successful call to a connection's Commit method, an application should call the CommitUpdates method to clear the cached update buffer.

Pay attention that running this method with Interbase and Firebird results in the following error: UniQuery1: Cannot perform this operation on a closed dataset . It occurs because InterBase and Firebird require an active transaction for any data operation. When you open a dataset without a transaction, UniDAC starts a transaction internally and allows you to read and write data. However, if you explicitly start a transaction and then either roll it back or commit it, the dataset will close (as there will be no active transaction). To avoid the error, you can use one of the following approaches:

1. Call the CommitRetaining method instead of Commit (or call the RollbackRetaining method for the except block instead of Rollback).permanently saves all changes associated with the transaction to the database server while retaining the transaction context. You can read more about CommitRetaining and RollbackRetaining in TUniConnection.CommitRetaining and TUniConnection.RollbackRetaining.

2. Use different transactions for connections and datasets. Additionally, use separate transactions for reading and modifying data. You can find a code example below.

Note: The preferred method for updating datasets is to call a connection component's ApplyUpdates method rather than to call each individual dataset's ApplyUpdates method. The connection component's ApplyUpdates method takes care of committing and rolling back transactions and clearing the cache when the operation is successful.

Example

The first part of the code illustrates how to apply a dataset's cached updates to a database with InterBase and Firebird in response to a button click, while the second part shows how to do the same without using InterBase and Firebird:

procedure ApplyButtonClick(Sender: TObject);
begin
  with Query1 do begin
    Connection1.StartTransaction;

    try
      // ... modify data
      ApplyUpdates;         // try to write the updates to the database
      Connection1.Commit;   // on success, commit the changes
    except
      RestoreUpdates;       // restore update result for applied records
      Connection1.Rollback; // on failure, undo the changes
      raise;                // raise the exception to prevent a call to CommitUpdates
    end;

    CommitUpdates;          //on success, clear the cache
  end;
end;

// use the different approach when working with InterBase/Firebird

// open the connection
...
  UniConnection1.AutoCommit := false;
  UniConnection1.Open;
...
  
// open the query  
...
  with UniQuery1 do begin
    CachedUpdates := true;
    Transaction := UniTransaction1;
    UpdateTransaction := UniTransaction2; // use different transaction for update
    Open;
  end;
...
    
procedure ApplyButtonClick(Sender: TObject);
begin
  with UniQuery1 do begin
    if not UpdateTransaction.Active then
      UpdateTransaction.StartTransaction;

    try
      // modify data
      ApplyUpdates;
      UpdateTransaction.CommitRetaining;
    except
      RestoreUpdates;
      UpdateTransaction.RollbackRetaining;
      raise;
    end;
    
    CommitUpdates;
  end;
end; 

See Also

© 1997-2024 Devart. All Rights Reserved. Request Support DAC Forum Provide Feedback