If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Connect to multiple instances of DB2 from a single C Application

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-19-04, 10:15
riteshukla riteshukla is offline
Registered User
 
Join Date: Feb 2004
Posts: 2
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);
strcpy(env_const,env_value);

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

/* Attach to the INSTANCE */
nodename=server;
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 */
sqledtin(&sqlca);
printf("sqlcode detach %d \n", sqlca.sqlcode);

I loop the above peice with a diferrent INSTANCE name.

THE PROBLEM:
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
while.
Any useful information or piece of code that accomplishes this would
be greatly appreciated.
Reply With Quote
  #2 (permalink)  
Old 02-19-04, 12:27
sathyaram_s sathyaram_s is offline
Super Moderator
 
Join Date: Aug 2001
Location: UK
Posts: 4,534
Re: Connect to multiple instances of DB2 from a single C Application

I saw this yesterday in Google Answers ... Am I right ? (It had 5 dollars reward against it )

By any chance do you have the full code (looks like it is missing the include ) ... This will give someone an opportunity to test it and check the problem

Sathyaram
__________________
Visit the new-look IDUG Website , register to gain access to the excellent content.
Reply With Quote
  #3 (permalink)  
Old 02-19-04, 14:00
riteshukla riteshukla is offline
Registered User
 
Join Date: Feb 2004
Posts: 2
Yeah, its the same one as in Google answers.

I had only given the piece of code as this is part of a bigger program.
Attaching the program file with can by tested .
Attached Files
File Type: c create_dblist.c (6.6 KB, 61 views)
Reply With Quote
  #4 (permalink)  
Old 02-19-04, 14:39
chuzhoi chuzhoi is offline
Registered User
 
Join Date: Dec 2002
Posts: 134
Re: Connect to multiple instances of DB2 from a single C Application

try using sqleBeginCtx/sqleEndCtx.

I think your problem is caching of instance variable by DB2. Mentioned above functions will work as a "terminate" equivalent

BTW, why do you need a C code for such task, you can easily implement a shell script to list all instances/all databases per instance.

regards,
dmitri
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On