Redirecting clients with application code
If you use the DBPATH environment variable or connectivity information to redirect connections, you can include in your clients a routine that handles errors when clients encounter a cluster failure. The routine can call another function that contains a loop that tries repeatedly to connect to the other database server in the cluster. This routine redirects clients without requiring the user to exit the application and restart it.
/* The routine assumes that the ONEDB_SERVER environment
* variable is set to the database server that the client
* normally uses and that the DBPATH environment variable
* is set to the other database server in the pair.
*/
#define SLEEPTIME 15
#define MAXTRIES 10
main()
{
int connected = 0;
int tries;
for (tries = 0;tries < MAXTRIES && connected == 0;tries++)
{
EXEC SQL CONNECT TO "superstores";
if (strcmp(SQLSTATE,"00000"))
{
if (sqlca.sqlwarn.sqlwarn6 != 'W')
{
notify_admin();
if (tries < MAXTRIES - 1)
sleep(SLEEPTIME);
}
else connected =1;
}
}
return ((tries == MAXTRIES)? -1:0);
}
EXEC SQL CONNECT TO "superstores@cliff_ol";
In
this example, superstores@cliff_ol
is a database
on a database server that the client computer recognizes. For redirection
to occur, the administrator must change the connectivity information
to make that name refer to a different database server. You might
be required to adjust the amount of time that the client waits before
it tries to connect or the number of tries that the function makes.
Provide enough time for an administrative action on the database server
(to change the connectivity information or change the type of the
secondary database server to standard).