Official Content

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

Scope

Platforms: Web(.Net, Java)

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 meet some requirements as well.

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

This procedure will be called every time 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 Datastore details, and you can modify those details depending on any logic that you may need to apply. For example, you can change the Datastore details depending on the User that is actually executing the application.

Furthermore, it must not access the database or use any functionality that does it. Some functionalities that access the database (and must not be used, because if they were used the program would enter an infinite loop) are as follows:

  • For Each command
  • Business Components
  • GAMSession.Get() or any method that internally accesses the database
  • Call to a procedure that accesses the database

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, you want to connect to different databases depending on the user logged in.

For this purpose, 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' 

Below is 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. Alternatives to connect to an external datasource and GX connection pool at runtime

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

See Also

DBConnection Data Type
After connect property

Last update: February 2024 | © GeneXus. All rights reserved. GeneXus Powered by Globant