Identify an explicit connection

From within the Informix® ESQL/C application, you can obtain the name of the database server and the name of the explicit connection with the GET DIAGNOSTICS statement.

When you use GET DIAGNOSTICS after an SQL connection statement (CONNECT, SET CONNECTION, and DISCONNECT), GET DIAGNOSTICS puts this database server information in the diagnostics area in the SERVER_NAME and CONNECTION_NAME fields.

The following code fragment saves connection information in the srvrname and cnctname host variables.
EXEC SQL connect to :dbname;
if(!strncmp(SQLSTATE, "00", 2)
   {
   EXEC SQL get diagnostics exception 1
       :srvrname = SERVER_NAME, :cnctname = CONNECTION_NAME;
   printf("The name of the server is '%s'\n", srvrname);
   }

From within the Informix® ESQL/C application, you can obtain the name of the current connection with the ifx_getcur_conn_name() function. This function returns the name of the current connection into a user-defined character buffer. The function is useful to determine the current connection among a group of active connections in a Informix® ESQL/C application that has multiple threads.

For example, the following code consists of a callback function, cb(), that two sqlbreakcallback() calls use in two different threads:
void
cb(mint status)
{
   mint res;
   char *curr_conn = ifx_getcur_conn_name();

   if (curr_conn && strcmp(curr_conn, "con2") == 0)
      {
      res = sqlbreak();
      printf("Return status of sqlbreak(): %d\n", res);
      }
}

void 
thread_1() 
{
EXEC SQL BEGIN DECLARE SECTION;
   mint res;
EXEC SQL END DECLARE SECTION;

   EXEC SQL connect to 'db' as 'con1' ;
   sqlbreakcallback(100, cb);
   EXEC SQL SELECT count(*) INTO :res FROM x, y;
   if (sqlca.sqlcode == -213)
      printf("Connection con1 fired an sqlbreak().\n");
   printf("con1: Result of count(*) = %d\n", res);
   EXEC SQL set connection 'con1' dormant ;
}

void 
thread_2() 
{
EXEC SQL BEGIN DECLARE SECTION;
   mint res;
EXEC SQL END DECLARE SECTION;

   EXEC SQL connect to 'db' as 'con2' ;
   sqlbreakcallback(100, cb);
   EXEC SQL SELECT count(*) INTO :res FROM x, y;
   if (sqlca.sqlcode == -213)
      printf("Connection con2 fired an sqlbreak().\n");
   printf("con2: Result of count(*) = %d\n", res);
   EXEC SQL set connection 'con2' dormant ;
}

The cb() callback function uses the ifx_getcur_conn_name() to check which connection is current.