Explicit connection management

The CONNECT statement is used to explicitly connect an application requester (AR) to an identified application server (AS). This SQL statement can be embedded within an application program or you can issue it using Interactive SQL.

The CONNECT statement is used with a TO or RESET clause. A CONNECT statement with a TO clause allows you to specify connection to a particular AS relational database. The CONNECT statement with a RESET clause specifies connection to the local relational database.

When you issue (or the program issues) a CONNECT statement with a TO or RESET clause, the AS identified must be described in the relational database directory. The AR must also be in a connectable state for the CONNECT statement to be successful.

The CONNECT statement has different effects depending on the connection management method you use. For RUW connection management, the CONNECT statement has the following effects:

For DUW connection management, the CONNECT statement has the following effects:

It is a good practice for the first SQL statement run by an application process to be the CONNECT statement. However, when you have CONNECT statements embedded in your program you might want to dynamically change the AS name if the program connects to more than one AS. If you are going to run the application at multiple servers, you can specify the CONNECT statement with a host variable as shown here, so that the program can be passed the relational database name.

CONNECT TO  : host-variable

Without CONNECT statements, all you need to do when you change the AS is to recompile the program with the new relational database name.

The following example shows two forms of the CONNECT statement (1 and 2) in an application program:

CRTSQLxxx PGM(SPIFFY/FIXTOTAL) COMMIT(*CHG) RDB(KC105)
 
  PROC: FIXTOTAL;
  EXEC SQL   CONNECT TO KC105; 1
  
  EXEC SQL
    SELECT * INTO :SERVICE
             FROM REPAIRTOT;
  
  EXEC SQL COMMIT;
  
  EXEC SQL CONNECT TO MPLS03 USER :USERID USING :PW;  2
  
  EXEC SQL SELECT …
  
  EXEC SQL COMMIT;
  
  END FIXTOTAL;

The example (2) shows the use of the USER/USING form of the CONNECT statement. You must specify the user ID and password with host variables when this form of the CONNECT statement is embedded in a program. If you are using TCP/IP, a user ID and password can be extracted from a security object at connect time if you have used the Add Server Authentication Entry (ADDSVRAUTE) command with the appropriate parameters to store them.

The following example shows both CONNECT statement forms in Interactive SQL. Note that the password must be enclosed in single quotation marks.

Type SQL statement, press Enter.
Current connection is to relational database (RDB) KC105.
CONNECT TO KC000_________________________________________________________

COMMIT___________________________________________________________________
===> CONNECT TO MPLS03 USER JOE USING 'X47K'_____________________________
_________________________________________________________________________
_________________________________________________________________________
 
Related concepts
Use the relational database directory
Related reference
Control DDM conversations