Building C/C++ multi-connection applications on Windows

You can use the provided build scripts to compile and link C and C++ embedded SQL and Db2® API programs. The scripts are in the sqllib\samples\c and sqllib\samples\cpp directories. The directories also contain sample programs that you can build with these files.

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

About this task

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 build script hardcodes default values of "sample" and "sample2" for the database names (%2 and %3) so if you are using the build script, and accept these defaults, you only have to specify the program name (the %1 parameter). If you are using the bldmc.bat script, 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, dbmcon.exe, you require two databases. If the sample database is not yet created, you can create it by entering db2sampl on the command line of a Db2 command window. The second database, here called sample2, can be created with one of the following commands:

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 node where the database resides.

Multi-connection also requires that the TCP/IP listener is running.

Procedure

To ensure that the TCP/IP listener is running:

  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 TCPIP_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 change the services file.
  3. Stop and restart the database manager in order for these changes to take effect:
       db2stop
       db2start

Results

The dbmcon.exe program is created from five files in either the samples\c or samples\cpp directories:
dbmcon.sqc or dbmcon.sqx
Main source file for connecting to both databases.
dbmcon1.sqc or dbmcon1.sqx
Source file for creating a package bound to the first database.
dbmcon1.h
Header file for dbmcon1.sqc or dbmcon1.sqx included in the main source file, dbmcon.sqc or dbmcon.sqx, for accessing the SQL statements for creating and dropping a table bound to the first database.
dbmcon2.sqc or dbmcon2.sqx
Source file for creating a package bound to the second database.
dbmcon2.h
Header file for dbmcon2.sqc or dbmcon2.sqx included in the main source file, dbmcon.sqc or dbmcon.sqx, for accessing the SQL statements for creating and dropping a table bound to the second database.
To build the multi-connection sample program, dbmcon.exe, enter:
   bldmc dbmcon sample sample2
The result is an executable file, dbmcon.exe.
To run the executable file, enter the executable name, without the extension:
   dbmcon

The program demonstrates a one-phase commit to two databases.