FireDAC.IFDPhysConnection.Pooling Sample
This sample describes how to use FireDAC in a multithreaded environment. This sample implements a multithreaded application, where each thread uses the IFDPhysConnection interface to establish a connection. The multiple connection establishments may lead to performance degradation across the whole system. To avoid this, you can enable the Pooled property to use the connection pooling.
Contents
Location
You can find the Pooling sample project at:
- Start | Programs | Embarcadero RAD Studio Alexandria | Samples and then navigate to:
Object Pascal\Database\FireDAC\Samples\Phys Layer\IFDPhysConnection\Pooling
- Subversion Repository:
- You can find Delphi code samples in GitHub Repositories. Search by name into the samples repositories according to your RAD Studio version.
Description
The Pooling sample shows you how to use the IFDPhysConnection interface together with the TThread class in order to establish connections to a database in a multithreaded application. To this end, the sample launches 10 threads. Each thread connects to the database and performs 50 SQL queries. In multithreaded applications, each thread starts and establishes a connection, performs a certain short task and then releases the connection. The repetitive connection establishments in multithreaded aplications may lead to performance degradation across the whole system. To avoid this, the application allows you to make use of the connection pooling. To show the benefits of the connection pooling, the sample compares and shows the time used to run the application with or without enabling the connection pooling.
How to Use the Sample
- Navigate to the location given above and open
IFDPhys_Pooling.dproj
. - Press F9 or choose Run > Run.
- Interact with the sample:
- Select an option from the Use Connection Definition combo box.
- Click the Run button and see the execution time.
- Select the Run Pooled check box, click the Run button and see the execution time.
- Compare both execution times.
Files
File in Delphi | Contains |
---|---|
|
The project itself. |
|
The main form. |
Implementation
When you run the application, you can interact with the sample using the following objects:
- A TComboBox object labeled as Use Connection Definition.
- Click the Use Connection Definition combo box and select an option in order to define a connection to a database. The menu shows all the persistent connections defined on the file
C:\Users\Public\Documents\Embarcadero\Studio\FireDAC\FDConnectionDefs.ini
. Once you select a connection definition, the sample enables the Run button and the Run Pooled check box.
- Click the Use Connection Definition combo box and select an option in order to define a connection to a database. The menu shows all the persistent connections defined on the file
- A TButton object labeled as Run.
- If you click the Run button, the sample launches 10 threads. Each thread uses the CreateConnection method of IFDPhysManager to create a connection to the database. Moreover, each thread uses the CreateCommand method of IFDPhysConnection to create a command for each connection. Finally, each thread uses the Prepare method of IFDPhysCommand to execute 50 SQL queries. The executed SQL query is the following
SELECT
command:'select count(*) from {id Region}'
. Therefore, in this case, each thread creates and uses a dedicated connection object working with the database.
- If you click the Run button, the sample launches 10 threads. Each thread uses the CreateConnection method of IFDPhysManager to create a connection to the database. Moreover, each thread uses the CreateCommand method of IFDPhysConnection to create a command for each connection. Finally, each thread uses the Prepare method of IFDPhysCommand to execute 50 SQL queries. The executed SQL query is the following
- A TCheckBox object labeled as Run Pooled.
- If you select this check box, the Pooled property of the connection setting is set to
True
. The database connection pooling is a method used to keep database connections open so they can be reused by others. Therefore, if you select the option, the threads can reuse the current opened connection.- Note: The connection pooling can be enabled only for a persistent or private connection definition.
- If you select this check box, the Pooled property of the connection setting is set to
- A TMemo object.
Uses
See Also
Samples
- FireDAC Connection Definitions sample
- FireDAC TFDConnection Pooling sample