Application Development Guide

Other Coding Considerations

This section documents additional considerations for implementing a UDF, items to keep in mind, and items to avoid.

Hints and Tips

The following are recommendations to consider to successfully implement your UDF:

UDF Restrictions and Caveats

This section discusses items to be avoided in your UDF:

  1. In general DB2 does not restrict the use of operating system functions. A few exceptions are:
    1. Registering of signal or exception handlers may interfere with DB2's use of these same handlers and may result in unexpected failure.
    2. System calls that terminate a process may abnormally terminate one of DB2's processes and result in system or application failure.

      Other system calls may also cause problems if they interfere with the normal operation of DB2; for example, a UDF that attempts to unload a library containing a UDF from memory could cause severe problems. Be careful in coding and testing any UDFs containing system calls.

  2. The values of all environment variables beginning with 'DB2' are captured at the time the database manager is started with db2start, and are available in all UDFs whether or not they are FENCED. The only exception is the DB2CKPTR environment variable. Note that the environment variables are captured; any changes to the environment variables after db2start is issued are not available to the UDFs.
  3. With respect to LOBs passed to an external UDF, you are limited to the maximum size specified by the UDF Shared Memory Size DB2 system configuration parameter. The maximum that you can specify for this parameter is 256M. The default setting on DB2 is 1M. For more information on this parameter, refer to the Administration Guide.
  4. Input to, and output from, the screen and keyboard is not recommended. In the process model of DB2, UDFs run in the background, so you cannot write to the screen. However, you can write to a file.
    Note:DB2 does not attempt to synchronize any external input/output performed by a UDF with DB2's own transactions. So for example, if a UDF writes to a file during a transaction, and that transaction is later backed out for some reason, no attempt is made to discover or undo the writes to the file.
  5. On UNIX-based systems, your UDF runs under the user ID of the DB2 Agent Process (NOT FENCED), or the user ID which owns the db2udf executable (FENCED). This user ID controls the system resources available to the UDF. For information on the db2udf executable, refer to the Quick Beginnings for your platform.
  6. When using protected resources, (that is, resources that only allow one process access at a time) inside UDFs, you should try to avoid deadlocks between UDFs. If two or more UDFs deadlock, DB2 will not be able to detect the condition.
  7. Character data is passed to external functions in the code page of the database. Likewise, a character string that is output from the function is assumed by the database to use the database's code page. In the case where the application code page differs from the database code page, the code page conversions occur as they would for other values in the SQL statement. You can prevent this conversion, by coding FOR BIT DATA as an attribute of the character parameter or result in your CREATE FUNCTION statement. If the character parameter is not defined with the FOR BIT DATA attribute, your UDF code will receive arguments in the database code page.

    Note that, using the DBINFO option on CREATE FUNCTION, the database code page is passed to the UDF. Using this information, a UDF which is sensitive to the code page can be written to operate in many different code pages.

  8. When writing a UDF using C++, you may want to consider declaring the function name as:
         extern "C" void SQL_API_FN udf( ...arguments... ) 
    

    The extern "C" prevents type decoration (or 'mangling') of the function name by the C++ compiler. Without this declaration, you have to include all the type decoration for the function name when you issue the CREATE FUNCTION statement.


[ Top of Page | Previous Page | Next Page ]