Recommended approach for using TMSQuery and TMSConnection

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
dougcl

Recommended approach for using TMSQuery and TMSConnection

Post by dougcl » Thu 11 Aug 2005 01:35

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

Paul
Posts: 725
Joined: Thu 28 Oct 2004 14:06

Post by Paul » Fri 12 Aug 2005 08:01

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.

Ludek

Post by Ludek » Wed 31 Aug 2005 17:34

I think you are missing the msquery.CommitUpdates statement! this should appear after database.commit.

Ludek

Post by Ludek » Wed 31 Aug 2005 17:38

Sorry, I can already see the CommitUpdates it in the quote...

Dougcl

Post by Dougcl » Thu 08 Sep 2005 17:59

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;

Ikar
Posts: 1693
Joined: Thu 28 Oct 2004 13:56

Post by Ikar » Fri 09 Sep 2005 14:20

It does not matter

Post Reply