CommitUpdates & RestoreUpdates

Discussion of open issues, suggestions and bugs regarding PgDAC (PostgreSQL Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
lauchuen
Posts: 37
Joined: Fri 07 Aug 2009 16:59

CommitUpdates & RestoreUpdates

Post by lauchuen » Sat 19 May 2012 10:46

hi,

do i have to perform CommitUpdates & RestoreUpdates everytime after Connection.Commit & Connection.Rollback (for example i have 3 dataset component)?

Code: Select all

DataSet.Connection.StartTranscation;
try
  DataSet1.ApplyUpdates;
  DataSet2.ApplyUpdates;
  DataSet3.ApplyUpdates;
  DataSet.Connection.Commit;
  DataSet1.CommitUpdates;
  DataSet2.CommitUpdates;
  DataSet3.CommitUpdates;
except
  DataSet.Connection.Rollback;
  DataSet1.RestoreUpdates;
  DataSet2.RestoreUpdates;
  DataSet3.RestoreUpdates;
  raise;
end;
any simplest way? thanks

ROD
Devart Team
Posts: 23
Joined: Mon 07 May 2012 09:09

Re: CommitUpdates & RestoreUpdates

Post by ROD » Wed 23 May 2012 08:10

Instead of calling ApplyUpdates for each DataSet, you can call ApplyUpdates for the connection. That is, change the code like this:

Code: Select all

DataSet.Connection.StartTranscation;
try
  DataSet.Connection.ApplyUpdates;
except
  DataSet.Connection.Rollback;
  DataSet1.RestoreUpdates;
  DataSet2.RestoreUpdates;
  DataSet3.RestoreUpdates;
  raise;
end;
Also, if you, for example, have to apply changes to a DataSet in different parts of the program, you can use only CommitUpdates (which will call ApplyUpdates itself, if it finds unapplied changes). So the code will be as follows:

Code: Select all

DataSet.Connection.StartTranscation;
try
  DataSet1.CommitUpdates;
  DataSet2.CommitUpdates;
  DataSet3.CommitUpdates;
  DataSet.Connection.Commit;
except
  DataSet.Connection.Rollback;
  DataSet1.RestoreUpdates;
  DataSet2.RestoreUpdates;
  DataSet3.RestoreUpdates;
  raise;
end;
RestoreUpdates in the except block must be called for every DataSet.

lauchuen
Posts: 37
Joined: Fri 07 Aug 2009 16:59

Re: CommitUpdates & RestoreUpdates

Post by lauchuen » Wed 23 May 2012 08:24

i think the second example will be good for me, because the application may contain Dataset4, Dataset5 in other from but using the same connection, if the first case will commit Dataset4, Dataset5 too, that i don't want to be.

i want to learn more about CommitUpdates, since refer to the document, its said will clear the dataset delta itself, is that means it will clear the delta after Connection.Commit ? or immediate clear the delta after call Dataset.CommitUpdates ?

thx.

ROD
Devart Team
Posts: 23
Joined: Mon 07 May 2012 09:09

Re: CommitUpdates & RestoreUpdates

Post by ROD » Wed 23 May 2012 10:08

If use CommitUpdates without ApplyUpdates, then it's better to do this before Connection.Commit, because when changes are not applied, it will explicitly call ApplyUpdates which requires Commit. Anyway, CommitUpdates can be put before Connection.Commit, since it just cleans the cache of changes.

Post Reply