[unixODBC-support] Checking connection status with ODBC

Alan J Batsford AJBatsford at uss.com
Wed Jan 14 15:28:30 GMT 2009


unixodbc-support-bounces at mailman.unixodbc.org wrote on 01/13/2009 12:13:21
PM:

> Alan J Batsford wrote:
> >
> > I'm using unixODBC 2.2.14 on CentOS 4.6. Right now I'm using ODBC to
> > connect to postgreSQL 8.2.5 within a C/C++ program, but the DSN is
> > going to change to Oracle soon. I can make a connection and run
> > queries. What's the best way to generically check the connection
status?
> >
> > Thanks for all your advise.
> >
>
> You could try using SQLGetConnectAttr with SQL_ATTR_CONNECTION_DEAD.
>
> --
> Peter
>

Thanks, I tried the function out but I can't quite get it to work.

SQLGetConnectAttr is returning -1 both when the connection is down and up.
When it's down I call SQLGetDiagRec and recieve a message about the
connection being down. But when the connection is up and I try to get the
diagnostic message I get an unreadable message. For example my last test
was:
"[nxDC[rvrMngrlvldatiueoto dniir"
with NativeError set to 0.

My call to SQLGetConnectionAttr:
char ODBC_str_err[20]; // Not sure how long this needed to be.
SQLINTEGER StringLength;

...

ODBC_erg = SQLGetConnectAttr(ODBC_Connection, SQL_ATTR_CONNECTION_DEAD,
(SQLPOINTER)ODBC_str_err, sizeof (ODBC_str_err), &StringLength);
if ((ODBC_erg == SQL_SUCCESS) || (ODBC_erg == SQL_SUCCESS_WITH_INFO))
{
      return 1;
}
else
{
      print_odbc_error(ODBC_Connection);
}

My code for checking the connection:
void print_odbc_error(SQLHDBC ODBC_Connection)
{
    SQLINTEGER ODBC_err = 0;
    SQLSMALLINT ODBC_mlen = 0;
    char ODBC_stat[10];
    char ODBC_msg[200];

    memset (ODBC_stat, 0, sizeof (ODBC_stat));
    memset (ODBC_msg, 0, sizeof (ODBC_msg));

    if (ODBC_Connection== 0)
    {
        fprintf("print_odbc_error failed due to ODBC Connection Handle ==
0.\n");
        return;
    }

    SQLGetDiagRec(SQL_HANDLE_DBC, ODBC_Connection, 1, ODBC_stat, &ODBC_err,
ODBC_msg, sizeof(ODBC_msg), &ODBC_mlen);
    fprintf("%s (%d)\n", ODBC_msg, ODBC_err);
}



Perhaps I'm using arguments wrong? Or the postgres ODBC driver doesnt
support it?

-Alan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.unixodbc.org/pipermail/unixodbc-support/attachments/20090114/12fff861/attachment.html>


More information about the unixODBC-support mailing list