Board index » delphi » Sharing TIBDatabase across DLLs

Sharing TIBDatabase across DLLs

Hi,
I have the following problem: I am working on a modular system - one
executable 'shell' which loads some module DLLs from its startup folder. The
shell contains a main form with a TIBDatabase and a TIBTransaction which
should be used by all modules.
All functions exported by the DLLs do the following:
1. Construct some forms with Application.CreateForm (note: in the
initialisation part of the DLLs I do Application.Handle := <Shell's
TApplication>.Handle);
2. Assign the main TIBDatabase and TIBTransaction from the shell to the
Database and Transaction properties of the TIBTable, TIBDataset and TIBQuery
components in the forms;
3. Show the forms in a modal state to perform some tasks;
4. Free the forms.

Now, the anomaly:
A function A in one of the DLLs calls a function B from another DLL. The
second function goes to the step 3 from above. Then at OnShow it tries to
open a TIBQuery. When I press F8 on the <...>.Open() call, the execution
point vanishes. When I switch to the debugged program, it has opened the
form, but the TIBQuery is closed and the rest of the code in OnShow has not
been executed.
- when I put the Open() in a try...except, it catches an exception: 'Dynamic
SQL Error: SQL Error code = -502: Declared cursor already exists'. Without
the try...except, the exception isn't handled at all (!?).
- when I call the function *for a second time* from A, it works well (but
the form was destroyed and recreated, so nothing should be left from the
first call)
- when I restart the whole thing and call the function B directly from the
main executable, it works. More puzzling, after that B works also when being
called from A for the first time!

Is there some special issue about sharing one TIBDatabase across several
DLLs? It's very important to me because it could prove necessary to redesign
the whole project.

TIA, Mikhail Balabanov

 

Re:Sharing TIBDatabase across DLLs


I don't know if this is the cause but I found the following: if I call
Randomize() before constructing the form in function B, it seems to run fine
when called from anywhere. Maybe this has something to do with the
generation of the random suffix of the name of the cursor used internally in
TIBQuery (same random sequence in each module if no Randomize?). The '-502'
error is gone now.

Re:Sharing TIBDatabase across DLLs


Hi,
I have already posted this problem to the attention of Jeff some week ago but
nothing, the solution I found is to export package.

Regards

Massimo

mago_merl...@bigfoot.com

Re:Sharing TIBDatabase across DLLs


You can not use one TIBDatabase over several threads. When two applications
are calling your DLL ther MUST be two instances of TIBDatabase and
TIBTransaction. Otherwise you will get troubles ....
You also MUST NOT use local Connections always use the Network sytax eg.
localHost:C:\AnyDir\Databse.GDB
else you will crash the GDS32.DLL

Bernhard Hartl
Bernhard.Ha...@fi-wa.com

Mikhail Balabanov <mis...@pracicorp.com> schrieb in im Newsbeitrag:
3a2fb2a5_2@dnews...

Quote
> Hi,
> I have the following problem: I am working on a modular system - one
> executable 'shell' which loads some module DLLs from its startup folder.
The
> shell contains a main form with a TIBDatabase and a TIBTransaction which
> should be used by all modules.
> All functions exported by the DLLs do the following:
> 1. Construct some forms with Application.CreateForm (note: in the
> initialisation part of the DLLs I do Application.Handle := <Shell's
> TApplication>.Handle);
> 2. Assign the main TIBDatabase and TIBTransaction from the shell to the
> Database and Transaction properties of the TIBTable, TIBDataset and
TIBQuery
> components in the forms;
> 3. Show the forms in a modal state to perform some tasks;
> 4. Free the forms.

> Now, the anomaly:
> A function A in one of the DLLs calls a function B from another DLL. The
> second function goes to the step 3 from above. Then at OnShow it tries to
> open a TIBQuery. When I press F8 on the <...>.Open() call, the execution
> point vanishes. When I switch to the debugged program, it has opened the
> form, but the TIBQuery is closed and the rest of the code in OnShow has
not
> been executed.
> - when I put the Open() in a try...except, it catches an exception:
'Dynamic
> SQL Error: SQL Error code = -502: Declared cursor already exists'. Without
> the try...except, the exception isn't handled at all (!?).
> - when I call the function *for a second time* from A, it works well (but
> the form was destroyed and recreated, so nothing should be left from the
> first call)
> - when I restart the whole thing and call the function B directly from the
> main executable, it works. More puzzling, after that B works also when
being
> called from A for the first time!

> Is there some special issue about sharing one TIBDatabase across several
> DLLs? It's very important to me because it could prove necessary to
redesign
> the whole project.

> TIA, Mikhail Balabanov

Re:Sharing TIBDatabase across DLLs


Thanks for the advice. Now I use
  <DLL_TIBDatabase>.SetHandle(<EXE_TIBDatabase>.Handle)
and together with the Randomize thingie all seems to run fine for now.

Re:Sharing TIBDatabase across DLLs


Quote
> You can not use one TIBDatabase over several threads. When two applications
> are calling your DLL ther MUST be two instances of TIBDatabase and
> TIBTransaction. Otherwise you will get troubles ....
> You also MUST NOT use local Connections always use the Network sytax eg.
> localHost:C:\AnyDir\Databse.GDB
> else you will crash the GDS32.DLL

I think that connect again at the same database, is not "politically correct"
:-) because on the connected user result two or more user.

Massimo

mago_merl...@bigfoot.com

Other Threads