DB2 Server for VSE & VM: Database Administration


Chapter 11. Stored Procedures


Stored Procedure Concepts

A stored procedure is a user-written application program that is compiled and stored at the server. When the database manager is running in multiple user mode, local applications or remote DRDA applications can invoke the stored procedure. Since the SQL statements issued by a stored procedure are local to the server, they do not incur the high network costs of distributed statements. Instead, a single network send and receive operation is used to invoke a series of SQL statements contained in the stored procedure.

There are several other benefits that can be gained through the use of stored procedures, including:


Stored Procedure Servers

The Stored Procedure Server

In DB2 Server for VSE & VM, all stored procedures are fenced, which means that they are separated from the database manager with respect to execution and memory usage. This is necessary to ensure that a stored procedure does not

A fenced implementation is achieved through the use of stored procedure servers. An stored procedure server is an application requester that is local to the database manager and is used to execute the stored procedure. A fenced implementation is achieved as follows:

Note that a stored procedure server must be dedicated to a single database server.

The Stored Procedure Handler

The stored procedure handler is a DB2 Server for VSE & VM supplied utility, called ARISPRC, that interfaces between the database manager and the stored procedure. It runs in the stored procedure server and does the following:

Stored Procedure Server Groups

The group clause of the CREATE PSERVER statement makes it possible to define groups of stored procedure servers. This is useful if

The group option gives the database administrator flexibility in defining the enviroment and is useful for system tuning.

Setting up a Stored Procedure Server

The SERVGROUP column in SYSTEM.SYSROUTINES is cross-referenced with the SERVGROUP column in SYSTEM.SYSPSERVERS to establish the server that is to be used for a stored procedure.

In DB2 Server for VM, the following requirements exist:

In DB2 Server for VSE, the following requirements exist:


Managing Stored Procedure Servers

Stored Procedure Server Allocation

When a stored procedure is running, the stored procedure server in which it is executing is dedicated to it. Since no other stored procedure can execute in that server until the current one finishes, it is not possible to execute multiple stored procedures concurrently with a single stored procedure server. However, multiple stored procedures can execute concurrently if multiple stored procedure servers are defined.

The CREATE PROCEDURE statement must be used to define a stored procedure before it can be used. The CREATE PROCEDURE puts the definition of the procedure into the catalog tables SYSTEM.SYSROUTINES and SYSTEM.SYSPARMS. See DB2 Server for VSE & VM SQL Reference for the definitions of these tables as well as more details on the CREATE PROCEDURE statement. A third catalog table, SYSTEM.SYSPSERVERS, is used to identify the stored procedure servers. See DB2 for VSE & VM SQL Reference for the definition of this table. When the database manager is started, DB2 Server for VSE & VM loads the contents of these tables into cached structures. The cached structures contain the information from the catalog tables, as well as information about the run time status of stored procedures and stored procedure servers. When the database manager is started, the status of a stored procedure defaults to STARTED, and the status of a stored procedure server defaults to STOPPED. The database manager then issues a START PSERVER command for any stored procedure server for which the AUTOSTART value of the corresponding row in SYSTEM.SYSPSERVERS is Y. When the START PSERVER command completes, the status of that stored procedure server is STARTING. When an SQL CALL statement is issued, the database manager uses the cached information to determine the server at which this stored procedure will run. To determine the server, DB2 Server for VSE & VM does the following. Note that when these steps make reference to the catalog tables, it is the cached information from the tables that they are referring to.

  1. Gets the value of the SERVGROUP column from the row in SYSTEM.SYSROUTINES for the procedure
  2. Looks for the first row in SYSTEM.SYSPSERVERS in which the value of the column SERVGROUP matches the SERVGROUP value retrieved from SYSTEM.SYSROUTINES, and which is not currently running a stored procedure. If one is found, the action taken by the database manager depends on the status of the procedure server:

If none of the servers in the group can be used, and the group checked was not the default group, DB2 Server for VSE & VM checks whether the stored procedure can run in the default group. Servers in the default group are indicated by a value of NULL for the SERVGROUP column in SYSTEM.SYSPSERVERS. If the DEFSERV column in SYSTEM.SYSROUTINES contains a 'Y' or is NULL, then that procedure can run in a server in the default group. If no servers are available in SERVGROUP, or if SERVGROUP in SYSTEM.SYSROUTINES is blank, and the procedure can run in the default group, DB2 Server for VSE & VM will attempt to run the stored procedure in one of the default servers. The process it uses to find a server in the default group is the same as the one it used to look for a server in a specific group, as described earlier.

If none of the servers at which it can run are available, the stored procedure waits for a free server. If more than one stored procedure is waiting for the same server, the one that has been waiting the longest will be invoked when the server is available.

Figure 57 shows how DB2 Server for VSE & VM resolves the server name. Note that in order to illustrate the process used, the figure shows cached data. Not all of the columns shown are in the corresponding catalog table.

Figure 57. How DB2 Server for VSE & VM Determines the Stored Procedure Server to Use


View figure.

The following steps are involved in Figure 57:

  1. SQL CALL PROC1 is issued. The database manager retrieves the SERVGROUP value from the row for PROC1 in the cached information from SYSTEM.SYSROUTINES.
  2. The database manager looks for the first row in the cached information from SYSTEM.SYSPSERVERS in which the SERVGROUP column matches the SERVGROUP value retrieved from the cached information for SYSTEM.SYSROUTINES and the PROC information is blank.
  3. The row for PSERVER SRV5 is found, but it is stopped and cannot be started implicitly.
  4. Since there are no other servers in GROUP1 that are not already running a stored procedure, the database manager checks the column DEFSERV to see if PROC1 can run in the default server group. A value of 'Y' or NULL in this column indicates that it can run in the default group.
  5. The SYSTEM.SYSPSERVERS data contains a value of NULL in the SERVGROUP column for any server in the default group. In this case, SRV1 and SRV2 are in the default server group and both are available. The database manager will use SRV1 since it is the first one found. It will update the cached information from SYSTEM.SYSPSERVERS to indicate that SRV1 is being used for PROC1, and send a command to SRV1 to cause it to invoke the stored procedure PROC1.

States of a Stored Procedure Server

A stored procedure server can be in several different states: STARTED, STARTING, STOPPING or STOPPED. Each state reflects/indicates the availability of the server and whether or not its definition can be altered or dropped. The START and STOP PSERVER operator commands can be used to change the state of a stored procedure server. An incoming SQL CALL request can also change the state of a stored procedure server, but only in certain situations, as will be described later. Following is a description of how each state is achieved and the functions that can be performed on the server for each of these states.

STOPPED

This is the default state for all stored procedure servers when the database manager is started. It is also achieved when a procedure server is stopped using the STOP PSERVER operator command. This state has two conditions that determine whether certain functions, such as an SQL CALL implicitly starting the server, are allowed. These two conditions are IMPLICIT and NOIMPLICIT. At startup, all stored procedure servers are STOPPED with IMPLICIT. Here is a summary of the differences between the two:

A stored procedure server will always be stopped if a severe error is encountered during the execution of a stored procedure. The IMPLICIT/NOIMPLICIT condition is not changed, unless a connection time-out has occurred, as mentioned above.

STOPPING

A server can only achieve this state if it was executing a stored procedure at the time the STOP PSERVER command was issued. When the state is "stopping", the database manager know that once the current SQL CALL requests concludes, the stored procedure server must be stopped. The stored procedure server will remain available as long as the IMPLICIT condition is true.

STARTING

In this state, the server is ready to start executing an SQL CALL request. It has been allocated a communication block but it is not yet connected. The database manager will establish the connection once an SQL CALL statement requests to use the server. If the connection is successful, the status will be changed to started. If it fails, the server will be stopped. The IMPLICIT/NOIMPLICIT condition will remain unchanged. If the connection times out, as mentioned earlier, the server will be stopped with the NOIMPLICIT condition true. A server in this state can also be stopped by issuing the STOP PSERVER operator command.

STARTED

In this state, the stored procedure server is connected to the database server and is either executing an SQL CALL statement, or is ready to execute an SQL CALL. This state can only be reached if an SQL CALL statement requested to use the server. The START PSERVER operator command will not promote a stored procedure server to this state as it does not establish the physical connection with the database server. The STOP PSERVER command demotes the server to a status of STOPPED and severs its connection if it is not currently executing an SQL CALL request. If the server is in use, the STOP PSERVER command will demote it to a status of STOPPING. The database server will complete the "stopping" sequence once the SQL CALL request concludes, by severing the stored procedure server's connection, freeing its communication block and changing the status to STOPPED. The IMPLICIT/NOIMPLICIT conditions will be dictated by the last STOP PSERVER operator command issued against the server.

For more information on the STOP and START PSERVER commands see the DB2 Server for VSE & VM Operation manual. For more information on the SQL CALL statement see the following manuals:

Altering or Dropping a Stored Procedure Server Definition

The following describes how to remove or alter a stored procedure server:

To remove a stored procedure server group, follow the steps above for each stored procedure server in the group. It is possible to remove all the servers in a group only if as there are no stored procedures defined that can use that server group. If this is the case, it is not be possible to remove the last procedure server in the group. Any stored procedures that run in the group must be moved to another group (by using the ALTER PROCEDURE satatement and specifying the SERVER GROUP clause) or dropped before the last stored procedure server in the group can be dropped.


Stored Procedures

Preparing a Stored Procedure to Run

Before a stored procedure can be invoked, it must be

If a stored procedure load module or phase is modified, a STOP PROC command must be issued, followed by a START PROC command, so that the database manager will cause the stored procedure server to load the new copy of the stored procedure load module or phase into memory.

After these steps are complete, the user that created the package associated with the stored procedure is able to GRANT RUN authority on the package to other users, allowing them to issue the SQL CALL statement to run the stored procedure.

Dropping or Altering a Stored Procedure

The following describes how to remove or alter a stored procedure:

Note: There is a special case where issuing the STOP PROC ACTION REJECT command will not suffice to allow altering or dropping a stored procedure definition. If the procedure is still running in a stored procedure server, you will not be allowed to alter or drop its definition, even if the status is STOP-REJ. The execution of an SQL ALTER or DROP PROCEDURE command will return with SQLCODE -15000. You can use the SHOW PSERVER and SHOW PROC operator commands to monitor the procedure's progress before you try to alter or drop its definition.


Initialization Parameters Affecting Stored Procedure Execution

PTIMEOUT Parameter

This parameter serves two purposes:

  1. The number of seconds before DB2 Server for VSE & VM ceases to wait for an SQL CALL to be assigned to a stored procedure server. If the PTIMEOUT interval expires, the SQL statement fails, and SQLCODE -913 is returned with SQLSTATE 40001.
  2. The number of seconds before DB2 Server for VSE & VM ceases to wait for the stored procedure server connection request to be established. If the PTIMEOUT interval expires, message ARI4168I is displayed and the connection attempt terminates. DB2 Server for VSE & VM will then try to use the next available stored procedure server, thus the SQL CALL request will not be terminated.

A value of 0 means that no PTIMEOUT is in effect. The default for PTIMEOUT is 180.

PROCMXAB Parameter

Specify the number of times a stored procedure is allowed to terminate abnormally, after which a STOP PROC ACTION REJECT is performed against the procedure and all subsequent SQL CALL statements are rejected. Note that a time-out that occurs while waiting for a stored procedure server to be assigned for an SQL CALL statement is not included in this count. The default, 0, means that the first abend of a stored procedure causes SQL CALLs to that procedure to be rejected. For production systems, you should accept the default.


Summary of Environment Interactions

Figure 58 shows the interactions between the database manager, the stored procedure server, the stored procedure handler, and the stored procedure itself. The figure does not show the definition of the stored procedure server or of the stored procedure itself; it is assumed that this has already been done.

Figure 58. Stored Procedure Environment


View figure.

  1. If the AUTOSTART value in the cached information from SYSTEM.SYSPSERVERS is Y, the database manager starts the stored procedure server during SQLSTART processing. If the AUTOSTART value is N, then the operator issues the START PSERVER command to start the stored procedure server. The START PSERVER command changes the status of the stored procedure server to STARTING. In VM, it also allocates a pseudoagent, and in VSE, it allocates a new XPCC block, both to be used for the connection between the database manager and the stored procedure server. See "The START PSERVER Command" in the DB2 Server for VSE & VM SQL Reference for more details.
  2. The user application at the application requester executes an EXEC SQL CALL statement.
  3. An SQL application can contain one or more SQL CALL statements. The SQL CALL statement is stored in a package in the DB2 Server for VSE & VM database using the DB2 Server for VSE & VM preprocessor. When the SQL CALL statement is received, the database manager consults the cached information from SYSTEM.SYSROUTINES and SYSTEM.SYSPARMS to:

    The database manager hooks the agent used by the requester that issued the SQL CALL to the selected stored procedure server. The database manager must maintain the CONNECT information for the original requester as well, in order to return the result of the SQL CALL statement.

    The database manager saves its environment in preparation for receiving and processing requests from the stored procedure. Finally it sends a request to the stored procedure handler to invoke the stored procedure.

  4. The stored procedure handler (ARISPRC) receives the request, and does the following:
  5. The stored procedure server effectively becomes a local requester and uses the connection that exists to communicate directly with the database manager. It uses private flows to execute the stored procedure. The database manager receives and processes the requests, and sends replies to the resource adapter. This continues until the stored procedure finishes. Note that the resource adapter is responsible for ensuring that disallowed statements are detected. For more details on the disallowed statements see "The SQL CALL" in the DB2 Server for VSE & VM SQL Reference.
  6. When the stored procedure terminates, control returns to ARISPRC.
  7. ARISPRC packages any output parameters and sends them to the database manager. Any cursors that were declared with the WITH RETURN option, and are left open when the stored procedure terminates, define result sets that can be fetched by the requester that issued the SQL CALL. Result sets are returned by the database server in the order the cursors were opened in the stored procedure. After sending the results to the database manager, ARISPRC cleans up the resource adapter environment and waits for the next request to invoke a stored procedure.
  8. When the database manager receives the stored procedure results from ARISPRC, it hooks the agent structure back to the original requester, and passes the stored procedure results back, using DRDA flows if necessary.


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