You are on page 1of 3
Show: 4 Delphi #) C++ Display Preferences Multithreading (FireDAC) From RAD Studio Go Up to Working with Connections (FireDAC) This topic describes how to use FireDAC in a multithreaded environment. Contents = 1 General = 2 Connection Pooling = 3 See Also = 3.1 Samples General ireDAC is thread-safe if the following conditions are met: = A connection object and all objects associated with it (such as TFDQuery, TFD Transaction, and so on) are used by a single thread at each moment. = FDManager is activated before threads start, by setting FDManager. Active to True. This means that after a thread opens a query and until its processing is finished, the application cannot use this, query and the connection objects in another thread. Similarly, after a thread starts a transaction and until the transaction is finished, the application cannot use this transaction and the connection objects in another thread. This practically means that an application must serialize access to a connection across all threads, which is not a convenient technique. Breaking these rules may lead to misbehavior, AV errors, and other errors, such as the SQL Server error "Connection is busy with results for another command”. The standard simplification is to create and use for each thread a dedicated connection object working with the database. In this case, no additional serialization is required. For example, the following code performs DB tasks in threads: fee : ‘ToBThread = elass(TThread) protected procedure Execute; override; end; ‘ocedure TOBThread. Execute; opre: TFOQuery; gin FreeonTerninate :+ False; ‘oconn := TFOCannection.Create(ndL) ‘eConn.ConnectiondefNane := ‘Oracle Pooled’; // see next ‘oPre i= TrOStoredProc .create(nil); oPre.connection := oconn; try @conn.Connected := True; 1 gere.Seoredoracname. =" MY_LONG_RURNING PROC" i oPre.ExecPracy finally oPresFrees end; ° na // main application code ‘oThreadi, oThread2: egin FoManager.active pthread othreads othread2 (DBTAread.Create(False); ToaTnread \Create(False); Sihreads.waitFors othreadi Free: othread2 WaitFors otheead2. Frees Sn Note: For the above case, where the application runs a single SQL query in the background, use the asynchronous query execution mode. Note: A multithreaded application may close the connections opened in the background threads in a TFDManager.BeforeShutdown event handler to avoid possible dead lock. Connection Pooling One of the expensive database interaction operations is the connection establishment. In a multithreaded application, where each thread starts, establishes a connection, performs a certain short task and releases the connection, the repetitive connection establishments may lead to performance degradation across the whole system. To avoid this, the application can use the connection pooling. The connection pooling can be enabled only for a persistent or private connection definition by setting Pooled=True. For a persistent definition: or for a private definition setup: ‘oParans: TStrings; ‘begin ‘OParans := TStringList.create; 1 oparans. tabase-ORA_920_ APP"); foParans..Add("User_Nane=A0ben0"); | oparams.Add( Password=a'); ‘Parans Add" 200led-'rue")3 Fowanager.AddconnectiondeF( "Oracle Pooled", ‘Ora', oParams); Foconne lend ;ConnactionDefNane := ‘Oracle Pooled"; nt connected = True; No additional parameters can be specified in the TFDConnection.Params property, because all pooled connections must share the same connection parameters. Setting TFDConnection.Connected to True acquires a physical connection from the pool. Setting TFDConnection. Connected to False releases the physical connection to the pool, but keeps the connection opened. To close and destroy all pooled physical connections, the application can call the TFDManager.CloseConnectionDef method: roManager.close: Additional connection definition parameters can be specified to set up a pool: Parameter Parameter Example POOL _CleanupTimeout The time (msees) until FireDAC removes the connections that have not been 3600000 used for longer than the POOL_ExpireTimeout time. The default value is 30000 msecs (30 secs). POOL _ExpireTimeout The time (msecs) after which the inactive connection may be deleted from 600000 the pool and destroyed. The default value is 90000 msecs (90 secs). POOL_Maximumltems The maximum number of connections in the pool. When the application 100 requires more connections, then an exception is raised. The default value is 50. See Also = Configuring Drivers = Defining Connection « Establishing Connection Samples = FireDAC IFDPhysConnection Pooling sample = FireDAC TFDConnection Pooling sample Retrieved from "http://doowiki.embarcadero.com/RADStudio/Berlinve/index.php? title-Multithreading_(FireDAC)&oldid=252457" Category: FireDAC = This page was last modified on 4 December 2015, at 05:17. = Help Feedback (QP, email)

You might also like