[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

> 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
> >
> > 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
"[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;


(SQLPOINTER)ODBC_str_err, sizeof (ODBC_str_err), &StringLength);
      return 1;

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 ==

    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?

-------------- 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