Subject RE: [IBO] info about error handling
Author Thomas Steinmaurer
Maik,

> Thanks's for the url I found a lot of helpful stuff about
> a lot of topics.
>
> It seems for me that the IB_Exception and it's direct subclasses are
> decendents of the Exception class in delphi and contain no information
> about the error wich was returned by the api.

The IBO base exception class is called EIB_Error, which is inherited
directly from Delphi's Exception class.

The exception class EIB_ISCError is inherited from EIB_Error and includes
everything you need. For eaxmple these properties:

- ERRCODE
- ErrorCodes
- ErrorMessage
- Sender
- SQL
- SQLCODE
- SQLMessage

Have a look in the help file or the Tech Info Sheet in my previous post.


> I will describe my problem more specefic.
>
> My application has a set of function wich are writing and
> reading data to and from a database using IB_Query,IB_Dsql,IB_StoredProc
> In normally I start a transaction and call a bunch of functions.
>
> If one of those functions fail I rollback the whole transaction and
> write an error into a log - no user can repeat this automatic process
> the data is lost.
>
> Normally this is no problem because the interface works pretty good and
> no errors happen.
>
> Now I have number of applications doing stuff at the same time
> without sync. The only problem happening is the deadlock error.

To avoid such deadlock scenarios, the first rule is to keep transactions as
short as possible. Don't start a transaction manually, update a bunch of
records and get a cup of coffee then, without committing the changes.

As long the write lock on these records isn't released (commit/rollback),
others can't update/delete these records, so you will get the scenario
described.

You might have a look on these Tech Info Sheets too:
http://www.ibobjects.com/TechInfo.html

- "Working with Transactions"
- "Transaction Tutorial"


> Now I want to build an error handling into the writing functions wich
> will repeat the call automatically after a deadlock until it succeeds.
>
> - Can I use one transaction for multiple function calls at a time and
> recover only the failed function ? (How)

No. InterBase/Firebird doesn't have something like nested transactions.
So, if you rollback your transaction, each operation in context of this
transaction will be rolled back.

For example, you have something similar like:

Start Transaction;
try
Operation 1;
Operation 2;
Operation 3; <-- Something went wrong here ...
Commit Transaction;
except
Rollback Transaction
end;

If Operation 3 fails, Operation 1 and 2 will be rolled back too ...


> - Or do I have to use a transaction for each function ?

Put all operations, which belong to a logical "grouped" operation into
one transaction. For example, creating an invoice from a delivery note
(with updating the articles stock, ...) should be done in one transaction.
If something went wrong when updating the stock, then the invoice
shouldn't be created too.

> - How can I get information about the Errorcode in the except block
> of my try-except statement ?

The EIB_ISCError class has everything you need. For example:


try
...
except
on E: EIB_ISCError do
// Use the properties of EIB_ISCError, like E.ErrorMessage ...
end;



HTH,
Thomas Steinmaurer
IB LogManager 2.1 - The Logging/Auditing Tool for InterBase and Firebird
http://www.iblogmanager.com