Building CLI multi-connection applications on Windows

Db2® provides batch files for compiling and linking CLI programs. These are located in the sqllib\samples\cli directory, along with sample programs that can be built with these files.

About this task

The batch file, bldmc.bat, contains the commands to build a Db2 multi-connection program requiring two databases. The compile and link options are the same as those used in bldapp.bat.

The first parameter, %1, specifies the name of your source file. The second parameter, %2, specifies the name of the first database to which you want to connect. The third parameter, %3, specifies the second database to which you want to connect. These are all required parameters.
Note: The makefile hardcodes default values of "sample" and "sample2" for the database names (%2 and %3) so if you are using the makefile, and accept these defaults, you only have to specify the program name (the %1 parameter). If you are using the bldmc.bat file, you must specify all three parameters.

Optional parameters are not required for a local connection, but are required for connecting to a server from a remote client. These are: %4 and %5 to specify the user ID and password for the first database; and %6 and %7 to specify the user ID and password for the second database.

For the multi-connection sample program, dbmconx, you require two databases. If the sample database is not yet created, you can create it by entering db2sampl on the command line. The second database, here called sample2, can be created with one of the following commands:

Procedure

  • If creating the database locally:
       db2 create db sample2
  • If creating the database remotely:
       db2 attach to <node_name>
       db2 create db sample2
       db2 detach
       db2 catalog db sample2 as sample2 at node <node_name>
    where <node_name> is the database partition where the database resides.
  • Multi-connection also requires that the TCP/IP listener is running. To ensure that the TCP/IP listener is running, follow the listed steps:
    1. Set the environment variable DB2COMM to TCP/IP as follows:
         db2set DB2COMM=TCPIP
      
    2. Update the database manager configuration file with the TCP/IP service name as specified in the services file:
         db2 update dbm cfg using SVCENAME <TCP/IP service name>
      
      Each instance has a TCP/IP service name listed in the services file. Ask your system administrator if you cannot locate it or do not have the file permission to read the services file.
    3. Stop and restart the database manager in order for these changes to take effect:
         db2stop
         db2start
    The dbmconx program consists of five files:
    dbmconx.c
    Main source file for connecting to both databases.
    dbmconx1.sqc
    Source file for creating a package bound to the first database.
    dbmconx1.h
    Header file for dbmconx1.sqc included in dbmconx.sqc for accessing the SQL statements for creating and dropping a table to be bound to the first database.
    dbmconx2.sqc
    Source file for creating a package bound to the second database.
    dbmconx2.h
    Header file for dbmconx2.sqc included in dbmconx.sqc for accessing the SQL statements for creating and dropping a table to be bound to the second database.
  • To build the multi-connection sample program, dbmconx, enter:
       bldmc dbmconx sample sample2
    The result is an executable file, dbmconx.
  • To run the executable file, enter the executable name:
       dbmconx
    The program demonstrates a two-phase commit to two databases.