RAD Studio
ContentsIndex
PreviousUpNext
Managing Multiple Sessions

If you create a single application that uses multiple threads to perform database operations, you must create one additional session for each thread. The BDE category on the Tool palette contains a session component that you can place in a data module or on a form at design time.

Warning: When you place a session component, you must also set its SessionName property to a unique value so that it does not conflict with the default session's SessionName property.
Placing a session component at design time presupposes that the number of threads (and therefore sessions) required by the application at runtime is static. More likely, however, is that an application needs to create sessions dynamically. To create sessions dynamically, call the OpenSession method of the global Sessions object at runtime. 

OpenSession requires a single parameter, a name for the session that is unique across all session names for the application. The following code dynamically creates and activates a new session with a uniquely generated name:  

Sessions.OpenSession('RunTimeSession' + IntToStr(Sessions.Count + 1));

 

Sessions->OpenSession("RunTimeSession" + IntToStr(Sessions->Count + 1));

This statement generates a unique name for a new session by retrieving the current number of sessions, and adding one to that value. Note that if you dynamically create and destroy sessions at runtime, this example code will not work as expected. Nevertheless, this example illustrates how to use the properties and methods of Sessions to manage multiple sessions. 

Sessions is a variable of type TSessionList that is automatically instantiated for BDE-based database applications. You use the properties and methods of Sessions to keep track of multiple sessions in a multi-threaded database application. The following table summarizes the properties and methods of the TSessionList component:  

TSessionList properties and methods  

Property or Method 
Purpose 
Count  
Returns the number of sessions, both active and inactive, in the session list.  
FindSession  
Searches for a session with a specified name and returns a pointer to it, or nil if there is no session with the specified name. If passed a blank session name, FindSession returns a pointer to the default session, Session.  
GetSessionNames  
Populates a string list with the names of all currently instantiated session components. This procedure always adds at least one string, "Default" for the default session.  
List  
Returns the session component for a specified session name. If there is no session with the specified name, an exception is raised.  
OpenSession  
Creates and activates a new session or reactivates an existing session for a specified session name.  
Sessions  
Accesses the session list by ordinal value.  

As an example of using Sessions properties and methods in a multi-threaded application, consider what happens when you want to open a database connection. To determine if a connection already exists, use the Sessions property to walk through each session in the sessions list, starting with the default session. For each session component, examine its Databases property to see if the database in question is open. If you discover that another thread is already using the desired database, examine the next session in the list. 

If an existing thread is not using the database, then you can open the connection within that session. 

If, on the other hand, all existing threads are using the database, you must open a new session in which to open another database connection. 

If you are replicating a data module that contains a session in a multi-threaded application, where each thread contains its own copy of the data module, you can use the AutoSessionName property to make sure that all datasets in the data module use the correct session. Setting AutoSessionName to True causes the session to generate its own unique name dynamically when it is created at runtime. It then assigns this name to every dataset in the data module, overriding any explicitly set session names. This ensures that each thread has its own session, and each dataset uses the session in its own data module.

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!