Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
4.96 Mб

Doing Callbacks with External C Procedures

lost RPC connection to external routine agent

This error, which means that agent extproc terminated abnormally because the external procedure caused a core dump. To avoid errors when declaring C prototype parameters, refer to the preceding tables.


To help you debug external procedures, PL/SQL provides the utility package DEBUG_EXTPROC. To install the package, run the script dbgextp.sql which you can find in the PL/SQL demo directory. (For the location of the directory, see your Oracle Database Installation or User's Guide.)

To use the package, follow the instructions in dbgextp.sql. Your Oracle Database account must have EXECUTE privileges on the package and CREATE LIBRARY privileges.

Note: DEBUG_EXTPROC works only on platforms with debuggers that can attach to a running process.

Demo Program

Also in the PL/SQL demo directory is the script extproc.sql, which demonstrates the calling of an external procedure. The companion file extproc.c contains the C source code for the external procedure.

To run the demo, follow the instructions in extproc.sql. You must use the SCOTT/TIGER account, which must have CREATE LIBRARY privileges.

Guidelines for External C Procedures

Handling Global Variables

A global variable is declared outside of a function, and its value is shared by all functions of a program. In case of external procedures, this means that all functions in a DLL share the value of the global. The usage of global variables is discouraged for two reasons:

Threading: In the non-threaded configuration of the agent process, there is only one function active at a time. However, in the case of multithreaded agents, multiple functions can be active at the same time. In that case, it is possible that

Calling External Procedures 8-51

Doing Callbacks with External C Procedures

two or more functions would try to access the global variable concurrently, with unsuccessful results.

DLL caching: Global variables are also used to store data that is intended to persist beyond the lifetime of a function. For example, consider two functions func1() and func2() trying to pass data to each other. Because of the DLL caching feature, it is possible that after func1()'s completion, the DLL will be unloaded, which results in all global variables losing their values. When func2() is executed, the DLL is reloaded, and all globals are initialized to 0, which will be inconsistent with their values at the completion of func1().

Handling Static Variables

There are two types of static variables: external and internal. An external static variable is a special case of a global variable, so its usage is discouraged. Internal static variables are local to a particular function, but remain in existence rather than coming and going each time the function is activated. Therefore, they provide private, permanent storage within a single function. These variables are used to pass on data to subsequent invocations of the same function. But, because of the DLL caching feature mentioned previously, the DLL might be unloaded and reloaded between invocations, which means that the internal static variable would lose its value.

See Also: Template makefile in the RDBMS subdirectory /public for help creating a dynamic link library

Guidelines for Call Specifications and CALL Statements

When calling external procedures:

Never write to IN parameters or overflow the capacity of OUT parameters. (PL/SQL does no run time checks for these error conditions.)

Never read an OUT parameter or a function result.

Always assign a value to IN OUT and OUT parameters and to function results. Otherwise, your external procedure will not return successfully.

If you include the WITH CONTEXT and PARAMETERS clauses, then you must specify the parameter CONTEXT, which shows the position of the context pointer in the parameter list.

If you include the PARAMETERS clause, and if the external procedure is a function, then you must specify the parameter RETURN in the last position.

8-52 Oracle Database Application Developer's Guide - Fundamentals

Doing Callbacks with External C Procedures

For every formal parameter, there must be a corresponding parameter in the PARAMETERS clause. Also, make sure that the datatypes of parameters in the PARAMETERS clause are compatible with those in the C prototype, because no implicit conversions are done.

With a parameter of type RAW or LONG RAW, you must use the property LENGTH. Also, if that parameter is IN OUT or OUT and null, then you must set the length of the corresponding C parameter to zero.

Restrictions on External C Procedures

The following restrictions apply to external procedures:

This feature is available only on platforms that support DLLs.

Only C procedures and procedures callable from C code are supported.

You cannot pass PL/SQL cursor variables or records to an external procedure. For records, use instances of object types instead.

In the LIBRARY subclause, you cannot use a database link to specify a remote library.

The maximum number of parameters that you can pass to a external procedure is 128. However, if you pass float or double parameters by value, then the maximum is less than 128. How much less depends on the number of such parameters and your operating system. To get a rough estimate, count each float or double passed by value as two parameters.

Calling External Procedures 8-53

Doing Callbacks with External C Procedures

8-54 Oracle Database Application Developer's Guide - Fundamentals

Part III

The Active Database

To take advantage of the reliability and performance of the database server, and to reuse program logic across all the applications in a database, you can move some of your program logic into the database itself, so that the database does cleanup operations and responds to events without the need for a separate application.

This part contains the following chapters:

Chapter 9, "Using Triggers"

Chapter 10, "Working With System Events"

Chapter 11, "Using the Publish-Subscribe Model for Applications"

Соседние файлы в папке Oracle 10g