FilreDAC DLL共享数据连接

时间:2022-11-29 04:59:35

D:\Users\Public\Documents\Embarcadero\Studio\16.0\Samples\Object Pascal\Database\FireDAC\Samples\Comp Layer\TFDConnection\DLL_Sharing

FDConnection1.CliHandle

FDConnection1.SharedCliHandle := ACliHandle;

CLI handle for connection sharing.

The CliHandle property returns a DBMS Call Level Interface connection handle. This value can be assigned to another TFDCustomConnection object's SharedCliHandle property. This is useful to transfer a connection from an application into a DLL. See DLL Development.

After setting Connected to True for the DLL connection, both connections will share the same physical DBMS connection. This connection must be closed after all other connections that share the same CLI handle.

CLI handle shared with other connections.

Use SharedCliHandle to set a shared DBMS Call Level Interface connection handle returned by another connection's CliHandle property. This is useful to transfer the connection from an application into a DLL (see DLL Development).

Note: A connection cannot be shared with another process, as the sharing works only inside the same address space.

After setting Connected to True for this connection, it will use the same physical DBMS connection or session as the other connection, and it will share the same transaction state.

After setting Connected to False, this DLL connection will not release the connection resources to the DBMC client. The application connection is responsible for resources releasing. Because of that, the application connection must be closed after closing all the sharing connections.

DLL Development (FireDAC)

From RAD Studio
 

Go Up to Working with Connections (FireDAC)

This topic describes how to use FireDAC in the dynamic loading libraries.

Contents

General

FireDAC may be used in a DLL as in a normal application. However, developers
must be aware of two techniques, specific to DLL development.

Connection Sharing Between an Application and a
DLL

When a connection must be transferred from an application to the DLL, the
application should not transfer the TFDCustomConnection
object, because this may lead to AV errors and other issues. This is not an
issue of FireDAC. It is due to the Delphi RTL / RTTI limitations. Note that a
connection cannot be shared with another process, because the sharing works only
inside the same address space.

To transfer a connection between an application and a DLL, the application
should transfer the TFDCustomConnection.CliHandle
property value to the DLL. Here the handle must be assigned to the TFDCustomConnection.SharedCliHandle
property.

After the TFDCustomConnection.SharedCliHandle
is assigned, the DLL connection can be activated by setting Connected to
True. Note that there is no need to set up a connection definition,
including DriverID. Then, the connection can be used as a normal database
connection. Finally, it can be closed by setting Connected to False. That
does not close the physical connection, therefore the application connection
remains active.

The application connection does not track state changes performed by the DLL.
Therefore, the DLL should preserve the same transaction state as it had before
the DLL call. It is indicated not to handle transactions in a DLL, change the
transaction isolation level and other settings in a DLL.

Also, setting SharedCliHandle does not transfer any of the option values from
an application to a DLL connection object. The DLL connection options can be set
similar to the application connection options.

For example, the DLL code: