INFORMIX
Informix-ESQL/C Programmer's Manual
Chapter 12: Working with the Database Server
Home Contents Index Master Index New Book

Using Database Server Control Functions

The following sections describe the ESQL/C library functions that you can use to control the database server sessions.

Function Name Description

ifx_getcur_conn_name()

Returns the name of the current connection.

sqgetdbs()

Returns the names of databases that a database server can access.

sqlbreak()

Sends the database server a request to stop processing.

sqlbreakcallback()

Establishes a time-out interval and a callback function for interrupting an SQL request.

sqldetach()

Detaches a child process from a database server connection.

sqldone()

Determines whether the database server is currently processing an SQL request.

sqlexit()

Terminates a database server connection.

sqlsignal()

Performs signal handling and cleanup of child processes.

sqlstart()

Starts a database server connection.

ifx_getcur_conn_name()

The ifx_getcur_conn_name() function returns the name of the current connection.

Syntax

Usage

The current connection is the active database server connection that is currently sending SQL requests to the database server and possibly receiving data from the database server. In a callback function, the current connection is the current connection at the time when the callback was registered with a call to the sqlbreakcallback() function. The current connection name is the explicit name of the current connection. If the CONNECT statement that establishes a connection does not include the AS clause, the connection does not have an explicit name.

Return Codes
Name of current
connection

Successfully obtained current connection name

Null pointer

Unable to obtain current connection name or current connection does not have an explicit name

sqgetdbs()

The sqgetdbs() function returns the names of databases that a database server can access.

Syntax

Usage

You must provide the following user-defined data structures to the sqgetdbs() function:

If the application is connected to a database server, a call to the sqgetdbs() function returns the names of the databases that are available in the database server of the current connection. This includes the user-defined databases as well as the sysmaster database. Otherwise, it returns the database names that are available in the default database server (that the INFORMIXSERVER environment variable indicates). If you use the DBPATH environment variable to identify additional database servers that contain databases, sqgetdbs() also lists the databases that are available on these database servers. It first lists the databases that are available through DBPATH and then the databases that are available through the INFORMIXSERVER environment variable.

Return Codes
0

Successfully obtained database names

<0

Unable to obtain database names

Example

The sqgetdbs.ec file in the demo directory contains this sample program.

For a source listing of the exp_chk() exception-handling function, see Chapter 11, "Exception Handling," of this manual.

Example Output

The output you see from the sqgetdbs sample program depends on how you set your INFORMIXSERVER and DBPATH environment variables. The following sample output assumes that the INFORMIXSERVER environment variable is set to mainserver and that this database server contains three databases that are called stores7, sysmaster, and tpc. This output also assumes that the DBPATH environment is not set.

sqlbreak()

The sqlbreak() function sends the database server a request to interrupt processing of the current SQL request. You generally call this function to interrupt long queries.

Syntax

Usage

The sqlbreak() function sends the interrupt request to the database server of the current connection. When the database server receives this request, it must determine if the SQL request is interruptible. Some types of database operations are not interruptible and others cannot be interrupted at certain points. You can interrupt the following SQL statements.

SELECT

OPEN

ALTER INDEX

UPDATE

CREATE TABLE

EXECUTE PROCEDURE

DELETE

CREATE INDEX

INSERT

ALTER TABLE

If the SQL request can be interrupted, the database server takes the following actions:

1. Discontinues execution of the current SQL request

    2. Sets SQLCODE (sqlca.sqlcode) to a negative value (-213)

    3. Returns control to the application

When the application regains control after an interrupted SQL request, any resources that are allocated to the SQL statement remain allocated. Any open databases, cursors, and transactions remain open. Any system-descriptor areas or sqlda structures remain allocated. The application program is responsible for the graceful termination of the program; it must release resources and roll back the current transaction.

While the database server executes an SQL request, the application is blocked, waiting for results from the database server. To call sqlbreak(), you must first set up some mechanism to unblock the application process. Two possible methods follow:

Before your program calls sqlbreak(), verify with the sqldone() function that the database server is currently processing an SQL request.

Return Codes

0

The call to sqlbreak() was successful. The database server connection exists and either a request to interrupt was sent successfully or the database server was idle.

!=0

No database server is running (no database connection exists) when you called sqlbreak().

sqlbreakcallback()

The sqlbreakcallback() function allows you to specify a time-out interval and to register a callback function. The callback function provides a method for the application to regain control when the database server is processing an SQL request.

Warning: Do not use the sqlbreakcallback() function if your ESQL/C application uses shared memory (olipcshm) as the nettype to connect to an Universal Server database server. Shared memory is not a true network protocol and does not handle the nonblocking I/O that is needed to support a callback function. When you use sqlbreakcallback() with shared memory, the call appears to register the callback function successfully (it returns zero); however, during SQL requests, the application never calls the callback function.

Syntax

Usage

Once you register a callback function with sqlbreakcallback(), the application calls this function at three different points in the execution of an SQL request. The value in the status argument of the callback function indicates the point at which the application calls the function. The following table summarizes the status values.

When Callback Function Is Called Value of status
Argument

When the database server begins processing an SQL request

status = 1

While the database server executes an SQL request, when the time-out interval has elapsed

status = 2

When the database server completes the processing of an SQL request

status = 0

When you call the callback function with a status value of 2, the callback function can determine whether the database server can continue processing with one of following actions:

The callback function, and any of its subroutines, can contain only the following ESQL/C control functions: sqldone(), sqlbreak(), and sqlbreakcallback(). For more information about the callback function, see page 12-38.

If you call sqlbreakcallback() with a time-out value of zero (0), the callback function executes immediately. The callback function executes over and over again unless it contains a call to sqlbreakcallback() to redefine the callback function with one of the following actions:

Important: Small time-out values might adversely affect the performance of your application.
For more information about the time-out interval, see page 12-37.

You must establish a database server connection before you call the sqlbreakcallback() function. The callback function remains in effect for the duration of the connection or until the sqlbreakcallback() function redefines the callback function.

Return Codes

0

The call to sqlbreakcallback() was successful.

<0

The call to sqlbreakcallback() was not successful.

sqldetach()

The sqldetach() function detaches a process from the database server. You generally call this function when an application forks a new process to begin a new stream of execution.

Syntax

Usage

If an application spawns one or more processes after it initiates a connection to a database server, all the child processes inherit that database server connection from the parent process (the application process that spawned the child). However, the database server still assumes that this connection has only one process. If one database server connection tries to serve both the parent and child processes at the same time, problems can result. For example, if both processes send messages to do something, the database server has no way of knowing which messages belong to which process. The database server might not receive messages in an order that makes sense and might thereby generate an error (such as error -408).

In this situation, call the sqldetach() function from the child process. The sqldetach() function detaches the child process from the connection that the parent process establishes (which the child inherits). This action drops all database server connections in the child process. The child process can then establish its own connection to a database server.

Use the sqldetach() function with the fork() system call. When you spawn a child process from an application process with a database server connection, sequence the function calls as follows:

1. Call fork() from the parent process to create a copy of the parent process (the child process). Now both parent and child share the same connection to the database server.

    2. Call sqldetach() from the child process to detach the child process from the database server. This call closes the connection in the child process.

Tip: You cannot use sqldetach() after a vfork() call because vfork() does not execute a true process fork until the exec() function is called. Do not use sqldetach() after the parent process uses an exec(); when exec() starts the child process, the child process does not inherit the connection that the parent process established.
A call to the sqldetach() function does not affect the database server sessions of the parent process. Therefore, after sqldetach() executes in the child process, the parent process retains any open cursors, transactions, or databases, and the child process has neither database server sessions nor database server connections.

When you call the sqlexit() function from the parent process, the function drops the connection in the parent process but does not affect the connections in the child process. Similarly, when you call sqlexit() from the child process, the function drops only the child connections; it does not affect the parent connections. The sqlexit() function rolls back any open transactions before it closes the connection.

If you execute the DISCONNECT statement from a child process, you disconnect the process from database server connections and terminate the database server sessions that correspond to those connections. The DISCONNECT fails if any transactions are open.

If the child process application has only one implicit connection before it calls sqldetach(), execution of the next SQL statement or of the sqlstart() library function reestablishes an implicit connection to the default database server. If the application has made one or more explicit connections, you must issue a CONNECT statement before you execute any other SQL statements.

The sqldetach demonstration program illustrates how to use the sqldetach() function.

Return Codes

0

The call to sqldetach() was successful.

<0

The call to sqldetach() was not successful.

Example

The sqldetach.ec file in the demo directory contains this sample program.

Example Output

sqldone()

The sqldone() function determines whether the database server is currently processing an SQL request.

Syntax

Usage

Use sqldone() to test the status of the database server in the following situations:

When the sqldone() function determines that the database server is not currently processing an SQL request, you can assume that the database server does not begin any other processing until your application issues its next request.

You might want to create a defined constant for the -439 value to make your code more readable. For example, the following code fragment creates the SERVER_BUSY constant and then uses it to test the sqldone() return status:

Return Codes

0

The database server is not currently processing an SQL request: it is idle.

-439

The database server is currently processing an SQL request.

sqlexit()

The sqlexit() function terminates all database server connections and frees resources. You can use sqlexit() to reduce database overhead in programs that refer to a database only briefly and after long intervals, or that access a database only during initialization.

Syntax

Usage

Only call the sqlexit() function when no databases are open. If an open database uses transactions, sqlexit() rolls back any open transactions before it closes the database. The behavior of this function is similar to that of the DISCONNECT ALL statement. However, the DISCONNECT ALL statement fails if any current transactions exist. Use the CLOSE DATABASE statement to close open databases before you call sqlexit().

If the application has only one implicit connection before it calls sqlexit(), execution of the next SQL statement or of the sqlstart() library function re-establishes an implicit connection to the default database server. If the application makes one or more explicit connections, you must issue a CONNECT statement before you execute any other SQL statements.

Return Codes

0

The call to sqlexit() was successful.

<0

The call to sqlexit() was not successful.

sqlsignal()

The sqlsignal() function enables, disables, or reenables signal handling of the signals that the ESQL/C library handles.

Syntax

Usage

The sqlsignal() function currently provides handling only for the SIGCHLD signal. In some instances, defunct child processes remain after the application ends. If the application does not clean up these processes, they can cause needless use of process IDs and increase the risk that you run out of processes. This behavior is only apparent when the application uses pipes for client-server communication (that is, the nettype field of the sqlhosts file is ipcpip). You do not need to call sqlsignal() for other communication mechanisms (for example, a nettype of tlipcp).

The mode argument of sqlsignal() determines the task that sqlsignal() performs, as follows:

    When you initialize signal handling with sqlsignal(), the ESQL/C library traps the SIGCHLD signal to handle the cleanup of defunct child processes. This initial call to sqlsignal() must occur at the beginning of your application, before the first SQL statement in the program. If you omit this initial call, you cannot turn on the signal-handling capability later in your program.

    If you want to have the ESQL/C library perform signal handling for portions of the program and your own code perform signal handling for other portions, you can take the following actions:

When you initialize SIGCHLD signal handling with sqlsignal(), you allow the ESQL/C library to process SIGCHLD cleanup. Otherwise, your application must perform the cleanup for these processes if defunct child processes are a problem.

sqlstart()

The sqlstart() function starts an implicit default connection. An implicit default connection can support one connection to the default database server (that the INFORMIXSERVER environment variable specifies).

Tip: Restrict use of sqlstart() to pre-Version 6.0 applications that only use one connection. ESQL/C continues to support this function for backward compatibility with these applications. For applications of Version 6.0 and later, use the CONNECT statement to establish explicit connections to a default database server.

Syntax

Usage

ESQL/C provides the sqlstart() function for pre-Version 6.0 applications that can only support single connections. In this context, possible uses of sqlstart() are as follows:

If you have a pre-Version 6.0 application that needs an implicit default connection for any other reason, use the DATABASE statement instead of sqlstart(). For applications of Version 6.0 and later, use the CONNECT statement to establish database server connections.

When you call the sqlstart() function, make sure that the application has not yet established any connections, implicit or explicit. When the application has established an explicit connection, sqlstart() returns error -1811. If an implicit connection has been established, sqlstart() returns error -1802.

You can call this function several times before you establish an explicit connection, as long as each implicit connection is disconnected before the next call to sqlstart(). For information on disconnecting, see "Terminating a Connection". For more information on explicit and implicit connections, see "Establishing a Connection".

Return Codes

0

The call to sqlstart() was successful.

<0

The call to sqlstart() was not successful.




Informix-ESQL/C Programmer's Manual, version 9.1
Copyright © 1998, Informix Software, Inc. All rights reserved.