IBM Books

Building Applications for UNIX** Environments


Chapter 4. Building AIX Embedded SQL Applications

This chapter provides detailed information for building embedded SQL applications on AIX. In the script files, commands that begin with db2 are Command Line Processor (CLP) commands. Refer to the Command Reference if you need more information about CLP commands.

General Points for Building and Running DB2 Programs

  1. You must build and run DB2 applications from a window where your environment variables are set. You can do this by running db2profile. Refer to "Setting Your Environment" if you need more information.

  2. To build DB2 programs containing embedded SQL, or to run any DB2 programs, the database manager on the server must be started. Start the database manager, if it is not already running, by entering the following command on the server:
    db2start
    

Considerations for running IBM and Micro Focus COBOL

Because of the way AIX loads stored procedures and resolves library references within them, there are requirements on how COBOL should be installed. These requirements become a factor when a COBOL program loads a shared library (stored procedure) at run time.

When a stored procedure is loaded, the chain of libraries it refers to must also be loaded. When AIX searches for a library only indirectly referenced by your program, it must use the path compiled into the library that referenced it when it was built by the language provider (IBM COBOL or Micro Focus COBOL). This path may very well not be the same path in which the compiler was installed. If the library in the chain cannot be found, the stored procedure load will fail, and you will receive SQLCODE -10013.

To ensure this does not happen, install the compiler wherever you want, then create symbolic links of all language libraries from the install directory into /usr/lib (a directory that is almost always searched when a library needs to be loaded). You could link the libraries into sqllib/function (the stored procedure directory), but this only works for one database instance; /usr/lib works for everyone on the machine. It is strongly recommended that you do not copy the libraries in; this especially applies to Micro Focus COBOL when multiple copies of the libraries exist.

A sample symbolic link of Micro Focus COBOL is provided below (assuming it is installed in /usr/lpp/cobdir):

    [1]> su root
    [2]> cd /usr/lib
    [1]> ln -sf /usr/lpp/cobdir/coblib/*.a .

About Stored Procedures and User-Defined Functions (UDFs)

Stored procedures are programs that access the database and return information to your client application. User-Defined Functions (UDFs) are your own scalar or table functions. Stored procedures and UDFs are compiled on the server, and stored and executed in a shared library on the server. This shared library is created when you compile the stored procedures and UDFs.

The shared library has an entry point, which is called from the server to access procedures in the shared library. The IBM XL C compiler on AIX allows you to specify any exported function name in the library as the default entry point. This is the function that is called if only the library name is specified in a stored procedure call or CREATE FUNCTION statement. This can be done with the -e option in the link step. For example:

-e funcname

makes funcname the default entry point. For information on how this relates to the CREATE FUNCTION statement, see "Relationship to Your CREATE FUNCTION Statement".

On other UNIX platforms, no such mechanism exists, so the default entry point is assumed by DB2 to be the same name as the library itself.

AIX requires you to provide an export file which specifies which global functions in the library are callable from outside it. This file must include the names of all stored procedures and/or user-defined functions in the library. Other UNIX platforms simply export all global functions in the library. This is an example of an AIX export file:



#! outsrv export file
outsrv

The export file outsrv.exp lists the stored procedure outsrv. The linker uses outsrv.exp to create the shared library outsrv that contains the stored procedure of the same name.
Note:After the shared library is built, it is typically copied into a directory from which DB2 will access it. When attempting to replace either a stored procedure or a user-defined function shared library, you should either run /usr/sbin/slibclean to flush the AIX shared library cache, or remove the library from the target directory and then copy the library from the source directory to the target directory. Otherwise, the copy operation may fail because AIX keeps a cache of referenced libraries and does not allow the library to be overwritten.

The AIX compiler documentation has additional information on export files.


[ Top of Page | Previous Page | Next Page | Table of Contents | Index ]

[ DB2 List of Books | Search the DB2 Books ]