Class DacSession() Professional
Class function of the DacSession class.
The DacSession class is used for session management. In addition to establishing and closing down connections to a server, the class also offers functionality to invoke a transaction processing system, if supported by the server.
The successful connection to a server marks the beginning of a session, which ends when the client application disconnects. While the session is open, all database operations triggered by database functions and commands such as USE database or DbCreate() are forwarded to the server by a DacSession object. The database operations are then performed by the server rather than by the client application. A DacSession object in turn receives the response from the server and returns it to the client application. This way, data sources residing on a server can be accessed as if they were stored on the computer running the application (client station).
An application may create multiple DacSession objects, and each object can handle a connection to a different server. In this case, the default session becomes important. Many functions and commands of the Xbase++ language do not allow to specify the server which is to perform a certain database operation. When the client application executes such a function or command, it is the DacSession object defined as the default session which accepts the request and forwards it to the server it is connected to. Also see the methods :setDefault() and :getDefault() for further information.
The default session has thread-local scope. This means that each thread in a multi-threaded program may have a different DacSession object defined as the default session. As a result, the database operations corresponding to a DbCreate() function call for example, could be performed by different servers if DbCreate() is used in threads with a different default session set.
#include "dac.ch"
PROCEDURE Main
LOCAL cConnect, oSession
// Ensure that PostgreSQL DatabaseEngine is loaded
// and set as the default dbe
IF !Dbeload( "PGDBE", .F. )
Alert( "PGDBE could not be loaded!" )
ENDIF
DbeSetDefault( "PGDBE" )
/* Connect to server:myserver and database:crm_system
* as user:uid with password:pwd
*/
cConnect := "DBE=PGDBE;SERVER=myserver;DB=crm_system;"
cConnect += "UID=username;PWD=password;"
oSession := DacSession():New( cConnect )
// Test for connection success
IF .NOT. oSession:isConnected()
? "Error code :", oSession:getLastError()
? "Description:", oSession:getLastMessage()
ELSE
? "Connection to server established."
ENDIF
// Your data access code goes here...
oSession:disconnect()
RETURN
#include "dac.ch"
PROCEDURE Main
LOCAL cConnect, oSession
// Ensure that ADS DatabaseEngine is loaded and
// set as the default dbe
IF !Dbeload( "ADSDBE", .F. )
Alert( "ADSDBE could not be loaded!" )
ENDIF
DbeSetDefault( "ADSDBE" )
/* Connect to server:lichtenstein and share:roy01
*/
cConnect := "DBE=ADSDBE;SERVER=\\LICHTENSTEIN\ROY01"
oSession := DacSession():New( cConnect )
// Test for connection success
IF .NOT. oSession:isConnected()
? "Error code :", oSession:getLastError()
? "Description:", oSession:getLastMessage()
ELSE
? "Connection to server established."
ENDIF
// Your data access code goes here...
oSession:disconnect()
RETURN
#include "dac.ch"
PROCEDURE Main
LOCAL cConnect, oSession
// Ensure that ODBC DatabaseEngine is loaded
// and set as the default dbe
IF !Dbeload( "ODBCDBE", .F. )
Alert( "ODBCDBE could not be loaded!" )
ENDIF
DbeSetDefault( "ODBCDBE" )
/* Connect to DBC as defined by SOURCEDB;
* use DELETED records, make visible in
* shared mode and use collation table
* "machine".
*/
cConnect := "DBE=ODBCDBE;DRIVER={Microsoft Visual FoxPro Driver};"
cConnect += "SOURCETYPE=DBC;SOURCEDB=c:\myvfp.dbc;Exclusive=No;"
cConnect += "NULL=no;COLLATE=machine;BACKGROUNDFETCH=no;DELETED=no;"
oSession := DacSession():New( cConnect )
// Test for connection success
IF .NOT. oSession:isConnected()
? "Error code :", oSession:getLastError()
? "Description:", oSession:getLastMessage()
ELSE
? "Connection to server established."
ENDIF
// Your data access code goes here...
oSession:disconnect()
RETURN
#include "dac.ch"
PROCEDURE Main
LOCAL cConnect, oSession
// Ensure that ODBC DatabaseEngine is loaded
// and set as the default dbe
IF !Dbeload( "ODBCDBE", .F. )
Alert( "ODBCDBE could not be loaded!" )
ENDIF
DbeSetDefault( "ODBCDBE" )
/* Connect to DSN:crm_system as
* username:uid with password:uid
* To set up a DSN (DataSoureName), use the
* ODBC Admin or Midex
*/
cConnect := "DBE=ODBCDBE;DSN=crm_system;UID=username;PWD=password"
oSession := DacSession():New( cConnect )
// Test for connection success
IF .NOT. oSession:isConnected()
? "Error code :", oSession:getLastError()
? "Description:", oSession:getLastMessage()
ELSE
? "Connection to server established."
ENDIF
// Your data access code goes here...
oSession:disconnect()
RETURN
#include "dac.ch"
PROCEDURE Main
LOCAL cConnect, oSession
// Ensure that ODBC DatabaseEngine is loaded
// and set as the default dbe
IF !Dbeload( "ODBCDBE", .F. )
Alert( "ODBCDBE could not be loaded!" )
ENDIF
DbeSetDefault( "ODBCDBE" )
/* Connect using the ODBC driver:DRIVER to
* the server:SERVER and the database:DATABASE
* using the given uid:username and pwd:password.
*/
cConnect := "DBE=ODBCDBE;DRIVER=SQL Server;"
cConnect += "SERVER=myserver;DATABASE=crm_system;UID=username;PWD=password;"
oSession := DacSession():New( cConnect )
// Test for connection success
IF .NOT. oSession:isConnected()
? "Error code :", oSession:getLastError()
? "Description:", oSession:getLastMessage()
ELSE
? "Connection to server established."
ENDIF
// Your data access code goes here...
oSession:disconnect()
RETURN
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.