Page 1 of 1

Recommended approach for using TMSQuery and TMSConnection

Posted: Thu 11 Aug 2005 01:35
by dougcl
Hello,
I am using cached updates with TMSQuery and TMSConnection. I am seeing strange errors. For details, see http://crlab.com/forums/viewtopic.php?t=1428

Instead of trying to figure out what is wrong, perhaps I can tackle this from another angle... can you guys at CORELAB please provide a recommended sequence of commands to commit cached updates?

I am currently doing this:
if msQuery.State in dsEditModes then begin
msConnection.StartTransaction;
try
msQuery.ApplyUpdates;
msConnection.Commit;
except
msConnection.RollBack;
end;
end;

Another question: I have noticed that sometimes (rarely) following the Commit, the msQuery.State is still equal to dsEdit! How can that happen? Should that happen?

Thanks,
Doug

Posted: Fri 12 Aug 2005 08:01
by Paul
We recomend the following sequence. You can find it in "TMemDataSet.ApplyUpdates" topic of SDAC help

Code: Select all

procedure ApplyButtonClick(Sender: TObject);

begin
  with MyQuery do
  begin
    Session.StartTransaction;
    try

... {Modify data}	

      ApplyUpdates; {try to write the updates to the database}
      Session.Commit; {on success, commit the changes}
    except
      RestoreUpdates; {restore update result for applied records}
      Session.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;
We cannot reproduce your problem with msQuery.State.
msQuery.ApplyUpdates always performs msQuery.Post operation.

Posted: Wed 31 Aug 2005 17:34
by Ludek
I think you are missing the msquery.CommitUpdates statement! this should appear after database.commit.

Posted: Wed 31 Aug 2005 17:38
by Ludek
Sorry, I can already see the CommitUpdates it in the quote...

Posted: Thu 08 Sep 2005 17:59
by Dougcl
Hi, thanks, I have made many changes to this sequence, and it all seems to be working now. One question though, your example shows CommitUpdates after the try block. Shouln't it be in the try block after DB.Commit? Like this:

procedure ApplyButtonClick(Sender: TObject);

begin
with MyQuery do
begin
Session.StartTransaction;
try

... {Modify data}

ApplyUpdates; {try to write the updates to the database}
Session.Commit; {on success, commit the changes}
CommitUpdates; {on success, clear the cache}

except
RestoreUpdates; {restore update result for applied records}
Session.Rollback; {on failure, undo the changes}
raise; {raise the exception to prevent a call to CommitUpdates!}
end;
end;
end;

Posted: Fri 09 Sep 2005 14:20
by Ikar
It does not matter