Subject Re: [IBO] Explicit transaction and FK Violation
Author Helen Borrie
At 09:57 PM 17-02-01 +1100, you wrote:


>Do it this way:
>
>procedure DoSQLWork( mySQL : String);
>begin
> with DataModule1.trWork do
> begin
> try
> if InTransaction then
> try
> Commit
> except
> Rollback;
> {{ and do something }};
> end;
> StartTransaction;
> with DataModule1.ibDSQLWork do
> begin
> if Prepared then
> Prepared := False;
> SQL.Clear;
> SQL.Add(mySQL);
> Prepare;
> Execute;
> end;{with ibDSQLWork}
> finally
> Commit;
> end;{try}
> end;{with trWork}
>end;{DoSQLWork}


Sorry, just noticed this try..finally.. which I mistakenly thought was a try...except. So here's replacement code for the end part:

> try

..........
> Prepare;
> Execute;
> end;{with ibDSQLWork}
> Commit

except
Rollback;
{{ handle the error }}
> end;{try}
> end;{with trWork}
>end;{DoSQLWork}

You should use try...finally to force the program to run a piece of code in the finally....end block *regardless* of what happens between try.... and finally, e.g. restore a screen cursor, release/free an object created by the method before the try... block began, etc.

With your use of finally... you are trying to run code which is actually impossible because the unhandled error inside the try... block makes the Commit impossible.

HB


All for Open and Open for All
InterBase Developer Initiative ยท http://www.interbase2000.org
_______________________________________________________