Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2005
    Posts
    1

    Unhappy Unanswered: Transactions and BDE a nightmare!

    Hi

    Here is the code I have currently. It is just a test program where I attempt to get the most basic transaction working:

    Query1.CachedUpdates := True;
    Database1.StartTransaction;
    { the test table has a code (pk), client_id, description, amount fields }
    try
    { take 100 rand from bob }
    tmp := 'insert into trans.db values (501, 100, "give2yoda", -100)';
    Query1.SQL.Clear;
    Query1.SQL.Add(tmp);
    Query1.ExecSQL;

    { sometimes I kill the application at this point - for testing }
    MessageDlg('Ok, kill me now if you want...', mtInformation, [mbOk], 0);
    { .... }

    { now give to yoda }
    tmp := 'insert into trans.db values (502, 500, "frombob", 100)';
    Query1.SQL.Clear;
    Query1.SQL.Add(tmp);
    Query1.ExecSQL;

    if Query1.UpdatesPending then
    begin { this block never gets executed, why????? }
    MessageDlg('Updates pending', mtInformation, [mbOk], 0);
    Query1.ApplyUpdates;
    Database1.Commit;
    Query1.CommitUpdates;
    end
    else
    MessageDlg('NO updated pending', mtInformation, [mbOk], 0);
    except
    Database1.Rollback;
    end;

    As I understand, the "Query1.CachedUpdates" is suppose to keep the inserts
    in a buffer, until " Query1.ApplyUpdates" actually writes it and
    "Database1.Commit" commits the changes. For some unkown reason, however, I
    keep on getting the message "No updates pending...". Furthermore, if I kill
    the application at the point indicated in the program, the one transaction
    is actually present in the table, and according to me, it shouldn't be
    there. Isn't it suppose to be all or nothing??

    Please help!!

    Regards

  2. #2
    Join Date
    Oct 2003
    Location
    Jordan
    Posts
    28

    Smile

    Hi

    Try to do like this

    Query1.CachedUpdates := True;
    Database1.StartTransaction;
    .
    .
    .
    .
    if Query1.UpdatesPending then
    Try
    Query1.CommitUpdates;
    Except
    End

    Try
    Database1.Commit;
    Except
    Database1.Rollback;
    end;
    Firas arramli
    Systems Analyst

  3. #3
    Join Date
    Oct 2003
    Posts
    84
    Cached updates are for when you actually use a query like
    SELECT * FROM Whatever;

    Assign a TUpdateSQL object to the TQuery's UpdateObject property, set CachedUpdates to TRUE, and open your dataset.

    Perform all your modifications using TQuery.Edit, TQuery.Insert, TQuery.Append....and set all the relevant field values. This is where CachedUpdates does its magic, if you call the Post method, the changes aren't applied to the underlying DB. At this point (after making at least one modification and calling Post (or moving to another record, which automatically cals Post), UpdatesPending will be TRUE.

    After you're done making changes you can call
    ApplyUpdates (which makes the changes to the underlying database) and
    CommitUpdates (which empties the cached of modifications, this empties out all the changes that were successfuly applied).

    IF you want to implement your DML statement inside a TQuery, you don't need cached updates.

    Read up on Cachedupdates in Delphi help files.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •