How to: Use Data Sessions
To ensure that each user in a shared environment has a secure, exact duplicate of the working environment, and to ensure that multiple instances of a form can operate independently, Visual FoxPro provides data sessions.
A data session is a representation of the current dynamic work environment. You might think of a data session as a miniature data environment running inside one open Visual FoxPro session on one machine. Each data session contains:
A copy of the items in the form's data environment.
Cursors representing the open tables, their indexes, and relationships.
The concept of a data session is easily understood when you consider what happens when you open the same form simultaneously from separate workstations in a multiuser application. In this case, each workstation is running a separate Visual FoxPro session, and therefore has its own set of work areas: cursors representing open base tables, indexes, and relationships. For more information about simultaneous access to data, see Updating Data Using Multiple Form Instances and Locking Data.
However, if you open multiple instances of the same form in a single project, on one machine, within the same Visual FoxPro session, the forms share the Default data session, representing a single dynamic work environment. Each instance of the open form open in the same Visual FoxPro session uses the same set of work areas, and actions in one instance of a form that move the record pointer in a work area automatically affect other instances of the same form.
Using Private Data Sessions
If you want to have more control over multiple instances of form, you can implement Private data sessions. When your form uses private data sessions, Visual FoxPro creates a new data session for each instance of the Form, FormSet, or Toolbar control your application creates. Each private data session contains:
A separate copy of each table, index, and relationship in the form's data environment.
An unlimited number of work areas.
Record pointers for each copy of each table that are independent from the base tables for the form.
The number of available data sessions is limited only by available system memory and disk space.
You implement private data sessions by setting the DataSession property for the form. The DataSession property has two settings:
1 – Default data session (the default setting).
2 – Private data session.
By default, the DataSession property of a form is set to 1.
To enable private data sessions
In the Form Designer, set the DataSession property of the form to 2 – Private data session.
-OR-
In code, set the DataSession Property to 2.
For example, type:
frmFormName.DataSession = 2
Note
You can only set the DataSession property at design time. The DataSession property is read-only at run time.
When a form uses private data sessions, each instance of a form open on a single machine in a single Visual FoxPro session uses its own data environment.
Note
The OPEN DATABASE command is not scoped to a data session. Unlike a table, a database, once opened, is available to all data sessions.
Equivalent multiple data sessions
Identifying Data Sessions
Each private data session is identified separately. You can see the contents of each data session in the Data Session Window. You can also change the data session description through commands in the Load event code.
To view the identification number for each data session
Use the run-time DataSessionID Property.
The following example displays the DataSessionID property of a form named frmMyForm:
DO FORM frmMyForm ? frmMyForm.DataSessionID
If you activate the form using the NAME clause, you can use the form's name to access the DataSessionID property, as in the following code:
DO FORM MyForm NAME one
? one.DataSessionID
The DataSessionID property is designed only to identify a particular data session. Avoid changing the DataSessionID of a form instance because data-bound controls lose their data sources when you change the DataSessionID.
Overriding Automatic Private Data Session Assignment
When private data sessions for a form are in use, changes you make to data in one form are not automatically represented in other instances of the same form. If you want all instances of a form to access the same data and to immediately reflect changes to common data, you can override automatic data session assignment.
To override automatic data session assignment
- Set the SET DATASESSION command to 1 or no value.
For example, the following code enables the Command window and Project Manager to control the default data session:
SET DATASESSION TO 1
-OR-
SET DATASESSION TO
See Also
Concepts
Customizing the Environment of a Data Session