Board index » delphi » Tricky transaction problem: Catching EDatabaseError from one of several Post within a transaction

Tricky transaction problem: Catching EDatabaseError from one of several Post within a transaction

On Mon, 10 Nov 1997 17:06:01 +0100, Anders Malmborg

Quote
<anders.malmb...@szg.externa.co.at> wrote:
>I believe that this must be a common problem:
>Two Datasets which both have been opened and put in append mode:

>Both Datasets are used in the same Form and should be storeed within one

>transaction.
>This is what I've tried:

>procedure TFrmDetail.BtnOkClick(Sender: TObject);
>begin
>   DB.StartTransaction;
>   try
>     Qry1.Post;
>     Qry2.Post;
>     DB.Commit;
>     ModalResult := mrOk;
>   except
>        DB.Rollback;
>   end;

>Now to the problem:
>If the first Post succeed, and the second not, the Qry1 is no more in
>append mode,
>and by next attempt to call Qry.Post the result will be an exception
>(Dataset is not in Edit or Insert mode).

>The next thing I did try was to re-append the dataset in the except
>section:
>   except
>        DB.Rollback;
>        if (Qry1.State <> dsInsert then
>            Qry1.Append;
>        if (Qry2.State <> dsInsert then
>            Qry2.Append;

>Then the values entered in the Form are cleared, which forces the user
>to reenter all values......

>Would appreciate if someone could give me some hints.

>Anders

perhaps you need to alter the start section
try
 qry1 post
except
 qry1.rollback;
 exit;
end;  // of try
// here only if first post is ok
try
 qry2 post;
except
 qry2.rollback;
 qry1.rollback;
 exit; // or raise ;?
end;
// here only if both posts ok
try
 qry1.commit;
 qry2.commit;
etc etc
// here
 

Re:Tricky transaction problem: Catching EDatabaseError from one of several Post within a transaction


I believe that this must be a common problem:
Two Datasets which both have been opened and put in append mode:

Both Datasets are used in the same Form and should be storeed within one

transaction.
This is what I've tried:

procedure TFrmDetail.BtnOkClick(Sender: TObject);
begin
   DB.StartTransaction;
   try
     Qry1.Post;
     Qry2.Post;
     DB.Commit;
     ModalResult := mrOk;
   except
        DB.Rollback;
   end;

Now to the problem:
If the first Post succeed, and the second not, the Qry1 is no more in
append mode,
and by next attempt to call Qry.Post the result will be an exception
(Dataset is not in Edit or Insert mode).

The next thing I did try was to re-append the dataset in the except
section:
   except
        DB.Rollback;
        if (Qry1.State <> dsInsert then
            Qry1.Append;
        if (Qry2.State <> dsInsert then
            Qry2.Append;

Then the values entered in the Form are cleared, which forces the user
to reenter all values......

Would appreciate if someone could give me some hints.

Anders

Re:Tricky transaction problem: Catching EDatabaseError from one of several Post within a transaction


On Mon, 10 Nov 1997 17:06:01 +0100, Anders Malmborg

Quote
<anders.malmb...@szg.externa.co.at> wrote:
>   except
>        DB.Rollback;
>        if (Qry1.State <> dsInsert then
>            Qry1.Append;
>        if (Qry2.State <> dsInsert then
>            Qry2.Append;

If you do Qry1.Append then you are actually appending another record.
What you should do is edit the current record.
Change Append to Edit in the above code.
This will allow you to re-execute Post without an exception and
without clearing the data from the field components.

Julian van Tubbergh.

Other Threads