Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2008

    Unanswered: Connect to multiple instances of DB2 from a single C Application

    Environment: AIX 5.1, language C, DB2 7.1
    This is what I am trying to do.

    I have more than one instance of DB2 on the server. Need to get the
    list of databases for each instance. The other instances(Nodes) are
    not cataloged in any instance, So I need to connect only through the
    env. variable DB2INSTANCE.

    This is how I am trying to do.
    1. Set environment variable DB2INSTANCE to the first instance.
    2. Attach to the first DB2 instance.
    3. Get the list of databases.
    4. Detach from Instance.
    5. Set environment variable DB2INSTANCE to the Second instance.
    2. Attach to the second DB2 instance.
    3. Get the list of databases.
    4. Detach from Instance.

    The code

    /*Set the environment variables which are used by sqlgetdbs() call to
    * connect to the server to fetch the datbases
    env_value = (char *)malloc(strlen(server)+16);
    strcpy(env_value, "DB2INSTANCE=");
    strcat(env_value, server);

    env_const = (char *)malloc(strlen(env_value)+1);

    if (putenv(env_const) != 0) {
    if (DEBUG) {
    printf("Error setting environment varible DB2INSTANCE\n");
    env_var = getenv("DB2INSTANCE");
    printf("env Var set value after: %s\n", env_var);

    /* Attach to the INSTANCE */
    sqleatin(nodename, NULL, NULL, &sqlca);
    printf("attach sqlcode %d\n",sqlca.sqlcode);

    instName[SQL_INSTNAME_SZ] = '\0';
    sqlegins (instName, &sqlca);
    printf("ins name sqlcode %d \n", sqlca.sqlcode);
    printf("ins name from sqlegins: %s \n", instName);

    /* open database directory scan */
    sqledosd(dbDirPath, &dbDirHandle, &nbDbEntries, &sqlca);
    /* printf("OPen sqlcode %d\n",sqlca.sqlcode);*/

    /* read the database entries */
    for (dbEntryNb = 0; dbEntryNb < nbDbEntries; dbEntryNb++)
    /* get next database directory entry */
    sqledgne(dbDirHandle, &dbEntry, &sqlca);
    printf("get sqlcode %d\n", sqlca.sqlcode);

    /* printing out the database information on to the screen */
    printf(" database name : %.8s\n", dbEntry->dbname);

    } /* end for */

    /* close database directory */
    sqledcls(dbDirHandle, &sqlca);
    printf("sqlcode close %d \n", sqlca.sqlcode);

    /* Detach from the instance */
    printf("sqlcode detach %d \n", sqlca.sqlcode);

    I loop the above peice with a diferrent INSTANCE name.

    This works fine for the first INSTANCE but it is fails to connect to
    the second instance and remains connected to the first one.

    Have tried alternate methods using multiple contexts, threads, etc.
    but have not been able to find a solution. This has troubled me for a
    Any useful information or piece of code that accomplishes this would
    be greatly appreciated.

  2. #2
    Join Date
    Oct 2008
    Anybody knows how to do this? I've been struggling to have it done. In my program, I'd like to collect information for each instance, programtically. Thank you.

  3. #3
    Join Date
    Oct 2008
    I saw somewhere says it's because DB2 is caching the DB2INSTANCE variable, that's why the second one will not work. Using multi context may solve the problem. I tried it and still not working.

  4. #4
    Join Date
    Jun 2003
    Toronto, Canada
    Provided Answers: 1
    See if this helps:

    "If directory caching is enabled, database, node, and DCS directory files are cached in memory. An application's directory cache is created during its first directory lookup. Since the cache is only refreshed when the application modifies any of the directory files, directory changes made by other applications may not be effective until the application has restarted. "
    "It does not work" is not a valid problem statement.

  5. #5
    Join Date
    Oct 2008
    Disabled directory cache and still doesn't work.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts