Database Engines:ads

Connection/Session management Professional

As outlined in the previous chapters, a connection to the Server is established using the DacConnect() class. In general objects of this class can be used to establish a connection to any server system supported by the DBE used.

Session with implicit connection

The DacSession() class provides two basic usage scenarios for establishing a connection. First using a connection string passed to the new() method when creating the object. Such as outlined below:

01: oSession := DacSession():New("DBE=ADSDBE;SERVER=\\ALASKA01\VOL1") 
02: oSession:IsConnected()  // .T. 

Session with explicit connection and session reuse

One can also create a session object without connecting to the server and later on perform an explicit connect. The session object can also be disconnected from the server and reused to establish a connection to a different server as shown below:

01: oSession := DacSession():New() 
02: oSession:Connect("DBE=ADSDBE;SERVER=\\ALASKA01\VOL1") 
03: oSession:IsConnected()  // .T. 
04: oSession:Disconnect() 
05: oSession:IsConnected()  // .F. 

DbUseArea() and DbCreate() implicit session context

Whenever a session is established this new session will implicitly become the default session of the current thread. To define a specific session the default of the current thread the :SetDefault() method must be executed. The following code illustrates this.

01: // create sessions and connect to servers 
02: oSession1 := DacSession():New("DBE=ADSDBE;SERVER=\\ALASKA01\VOL1") 
03: oSession2 := DacSession():New("DBE=ADSDBE;SERVER=\\CANADA\VOL1") 
04: 
05: // now create a new table using server ALASKA01 
06: oSession1:SetDefault() 
07: DbCreate("TheBear",aStruct,"ADSDBE") 
08: 
09: // open a table with the CANADA server 
10: oSession2:SetDefault() 
11: DbUseArea(.T.,"ADSDBE", "Citizen") 

The implicit session usage of DbUseArea() and DbCreate() provides certain benefits when having just a single connection to a server and using only the ADS DatabaseEngine. Whenever different DatabaseEngines are used, the DBE should be specified in order to avoid any dependency from the current default DatabaseEngine.

Explicit session context for DbUseArea() and DbCreate()

The following code shows how the second parameter of DbUseArea() or the third parameter at DbCreate() can be used to specify the session context for the operation. <oSession> optionally specifies the session context for the operation. <oSession> must be a object of DacSession() or any derived class. If <oSession> is not specified the current session of the current thread is used. More information about this topic can be found in the Xbase++ reference documentation.

01: // create sessions and connect to servers 
02: oSession1 := DacSession():New("DBE=ADSDBE;SERVER=\\ALASKA01\VOL1") 
03: oSession2 := DacSession():New("DBE=ADSDBE;SERVER=\\KANADA\VOL1") 
04: 
05: // now create a new table using server ALASKA01 
06: DbCreate("TheBear",aStruct,oSession1) 
07: 
08: // open a table with the KANADA server 
09: DbUseArea(.T., oSession2 , "Citizen") 

The benefit of using the <oSession> parameter for DbUseArea() and DbCreate() is that there is no need to switch the context on various Session objects. Passing the session object to the function clearly defines the context in which the operation has to be executed. In addition there is no need to specify the DatabaseEngine (DBE) explicitly. As the Session knows already the DatabaseEngine and therefore defines the DatabaseEngine and the session context for the DbUseArea() or DbCreate() operation.

Feedback

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.