Collection Contents Index Using host variables Fetching data pdf/chap2.pdf

Programming Interfaces Guide
   CHAPTER 2. The Embedded SQL Interface     

The SQL Communication Area


The SQL Communication Area (SQLCA) is an area of memory that is used on every database request for communicating statistics and errors from the application to the database server and back to the application. The SQLCA is used as a handle for the application-to-database communication link. It is passed in to all database library functions that need to communicate with the database server. It is implicitly passed on all Embedded SQL statements.

A global SQLCA variable is defined in the interface library. The preprocessor generates an external reference for the global SQLCA variable and an external reference for a pointer to it. The external reference is named sqlca and is of type SQLCA. The pointer is named sqlcaptr. The actual global variable is declared in the imports library.

The SQLCA is defined by the sqlca.h header file, included in the h subdirectory of your installation directory.

SQLCA provides error codes 

You reference the SQLCA to test for a particular error code. The sqlcode and sqlstate fields contain error codes when a database request has an error (see below). Some C macros are defined for referencing the sqlcode field, the sqlstate field, and some other fields.

Top of page  Fields in the SQLCA

The fields in the SQLCA have the following meanings:

sqlerror array 

The sqlerror field array has the following elements.

Top of page  SQLCA management for multi-threaded or reentrant code

You can use Embedded SQL statements in multi-threaded or reentrant code. However, if you use a single connection, you are restricted to one active request per connection. In a multi-threaded application, you should not use the same connection to the database on each thread unless you use a semaphore to control access.

There are no restrictions on using separate connections on each thread that wishes to use the database. The SQLCA is used by the runtime library to distinguish between the different thread contexts. Thus, each thread wishing to use the database must have it's own SQLCA. Any given database connection will only be accessible from one SQLCA.

Top of page  Using multiple SQLCAs

  To manage multiple SQLCAs in your application:
  1. You must use the command line switch on the SQL preprocessor that generates reentrant code (-r). The reentrant code is a little larger and a little slower because statically initialized global variables cannot be used. However, these effects are minimal.

  2. Each SQLCA used in your program must be initialized with a call to db_init and cleaned up at the end with a call to db_fini.

    Caution    
    Failure to call db_fini for each db_init on NetWare can cause the database server to fail, and the NetWare file server to fail.

  3. The Embedded SQL statement SET SQLCA (SET SQLCA statement) is used to tell the SQL preprocessor to use a different SQLCA for database requests. Usually, a statement such as: EXEC SQL SET SQLCA 'task_data->sqlca'; is used at the top of your program or in a header file to set the SQLCA reference to point at task specific data. This statement does not generate any code and thus has no performance impact. It changes the state within the preprocessor so that any reference to the SQLCA will use the given string.

For Info     For information about creating SQLCAs, see SET SQLCA statement.

Top of page  When to use multiple SQLCAs

You can use the multiple SQLCA support in any of the supported Embedded SQL environments, but it is only required in reentrant code.

The following list details the environments where multiple SQLCAs must be used:

Top of page  Connection management with multiple SQLCAs

You do not need to use multiple SQLCAs to connect to more than one database or have more than one connection to a single database.

Each SQLCA can have one unnamed connection. Each SQLCA has an active or current connection (see SET CONNECTION statement). All operations on a given database connection must use the same SQLCA that was used when the connection was established.

Record locking    
Operations on different connections are subject to the normal record locking mechanisms and may cause each other to block and possibly to deadlock. For information on locking, see the chapter Using Transactions and Locks.

Top of page  

Collection Contents Index Using host variables Fetching data pdf/chap2.pdf