Transaction Implicit versus Explicit


Standard Insert, Delete and Modify operation done on a None Transaction based Table or Query uses the Implicit transaction control.


for Batch update processes and Hardcopy generation(Reporting) processes will undoubtedly require explicit transaction control. When in doubt, it is probably best to control the transactions yourself.


third alternative: you can use pass-through SQL to send transaction control commands directly to the back-end server. Keep in mind that when you do this, your application code is now dependent on the back-end server.


In order to use SQl pass-through to control transaction, you must set the SQLPAssThroughMode of alias in use to Shared NoAutoCommit under the Borland Database Engine(BDE) configuration program, which tell's BDE to not automatically commit passthrough statements.



sample code below demonstrate basic Transaction handling:


procedure TForm1.ChangeAmount(amt: real);

begin

        { start a DataBase transaction and disable to prevent screen flicker }

        DBChap24.StartTransaction;

        TBOrders.Disablecontrols;


        { goto to 1st record }

        TBOrders.first;


        { iterate throught all records }

        while not TBOrders.Eof do

        begin        

                { update the status line }

                StatusPanel.caption := 'updating order ' + TBOrdersORDERNO.AsString;                

                StatusPanel.Repaint;


        {      edit the current record, increase the amount, move to next record pls. note that TTable component

                name was used and not the field names

        }

                TBOrders.Edit;

                TBOrdersAmountPaid.Value := TBOrdersAmountPaid.Value * amt;

                TBOrders.next;                

        end;


        { ask user to save }

        if messageDlg('Commit now ?', mtConfirmation, [mbYes, mbNo], 0) = mrYes then

                DBChap24.Commit;

        else

                begin

                   DBChap24.Rollback;

                   StatusPanel.Caption :=  'Changes rolled back';        

                end;

                

        TBOrders.enablecontrols;

end;

                        


To give us some control over the optimistic updating scheme on client/server data. Delphi gives us the UpdateMode property on the TTable and TQuery objects.


upWhereAll - update by using every column to find the record to update. If another user changed any field in the record, the update will fail.


upWhereChanged - update using only changed columns to find the record to update. will fail only if another user changed the same column that was subjected for update.


upWhereKeyOnly - update by using only the key fields to find the record to update. as long as other users do not change the key fields, then each user will overwrite changes to the other. Not recommended...