informix
INFORMIX-ESQL/C Programmer's Manual
ESQL/C Function Library

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.
  3. 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.

Output


INFORMIX-ESQL/C Programmer's Manual, Version 9.21
Copyright © 1999, Informix Software, Inc. All rights reserved