Before connect property

Official Content
This documentation is valid for:
The Before Connect property has been implemented to change connection properties dynamically at execution time. In order to use this feature, a GeneXus procedure is required. This procedure should state the connection properties at execution time; it will be executed immediately before any connection request is executed by the application to interact with the database.

Description

A GeneXus Procedure object should be stated in the 'Before Connect' property because it will be called before the connection is established. This procedure must be programmed in GeneXus and should fulfill some requirements as well.

In this procedure you may state/change the properties of the Data store to which the application should connect to.

This procedure will be called every time that a database request is executed. For instance, if we have a Web Panel object that loads data from a database, this procedure will be executed in each Refresh (F5).

Procedure requirements

It must have at least one in/out DBConnection type parameter.

parm(INOUT: &dbconn); //&dbconn is a DBConnection type variable

In 3 Tier models, the Execute in new LUW property must be set to Yes, in Web environments, it must be set to No.

The procedure receives the &dbconn with the Data store details, and you can modify those details depending on any logic that you may need to apply. For example, you can change the Data store details depending on the User that is actually executing the application.

Samples

Multi company application

Suppose that the Login Web Panel sets the user that has logged in a web session:

&Session.Set('UserID', str(&UserID))

Also, we want to connect to different databases depending on the user logged in.

For this purpose, let's define a Procedure named CONNECT, which must be stated in the 'Before Connect' Model property:

JAVA Example

Sample I.

Rules

Parm(inout: &dbconn); 

Source

&UserID = val(&Session.Get('UserID')) 
Do Case
   Case &UserID = 1 
        &DataBase  = "companyone" 
   Case &UserID = 2 
        &DataBase  = "companytwo" 
   Otherwise 
        &DataBase  = "companyone" //default database 
EndCase

// Changing Default connection 
&dbconn = GetDatastore("Default") 
&dbconn.JDBCDriverName = "com.microsoft.jdbc.sqlserver.SQLServerDriver" 
&dbconn.JDBCDriverURL = "jdbc:microsoft:sqlserver://MyServer:1433;databaseName=" + trim(&DataBase) + ";SelectMethod=cursor" 
&dbConn.UserName = 'username' 
&dbConn.UserPassword = 'userpassword' 

The same case but using JDBC driver: 

&dbconn.JDBCDriverName = "net.sourceforge.jtds.jdbc.Driver"
&dbconn.JDBCDriverURL = "jdbc:jtds:sqlserver://MyServer:1433/"+ &database.Trim()

 

Sample II. Alternates to connect to an external datasource and GX connection pool in run-time

Rules

Parm(inout: &dbconn); 

Source  

&dbconn = GetDatastore("Default")
 
if &Connection = 'JNDI'
   &dbconn.UseExternalDatasource = 1
   &dbconn.ExternalDatasourceName = 'java:/comp/env/jdbc/myoracle' 
else
   &dbconn.UseExternalDatasource = 0
   &dbconn.JDBCDriverName = "oracle.jdbc.driver.OracleDriver"
   &dbconn.JDBCDriverURL = "jdbc:oracle:thin:@Testorcl:1521:testorcl"
   &dbConn.UserName = &UserId.Trim()
   &dbConn.UserPassword = &UserPwd.Trim()
endif

.NET Example

Rules

Parm(inout: &dbconn); 

Source

&UserID = val(&Session.Get('UserID')) 
//select the Database depending on UserID 
Do Case
   Case &UserID = 1 
        &DataBase  = "companyone" 
   Case &UserID = 2 
        &DataBase  = "companytwo" 
   Otherwise 
        &DataBase  = "companyone" //default database 
EndCase

//Change connection properties 
&dbconn = GetDatastore("Default") 
&dbconn.UserName = 'username' 
&dbconn.UserPassword = 'userpassword'
&dbconn.ConnectionData = "DATABASE=" + &DataBase.Trim() //SQLServer 
//&dbconn.ConnectionData = "Data Source=" + &DataBase.Trim()  //MySQL

Scope

Platforms: Web(.Net, Java)

See Also

DBConnection Data Type
After connect property

Was this page helpful?
What Is This?
Your feedback about this content is important. Let us know what you think.