Connection Timeout and ODBC Native Client RRS feed

  • Question

  • I use ODBC SQL Native Client to connect to SQL Server.

    When I call SQLDriverConnect to connect to an SQL Server instance and  server is down, the function takes 20+ seconds to return. I want to be able to detect it sooner, lets say after 3 secs, but there is no connection timeout property like there is in .NET SqlClient or OLEDB using Native Client.

    I tried using SQL_ATTR_LOGIN_TIMEOUT but it is not implemented in SQL Native Client. I also tried to enable asynchronous mode for connecting using SQLSetConnectAttr and SQL_ATTR_ASYNC_ENABLE but still the delay is the same. The login doesn't seem to happen asynchronously.

    Is there any way using ODBC and Native Client to detect that the server is down, or get a login timeout sooner? Maybe some configuration in the registry or the server? My application cannot wait for so long when there is a problem. I am using also ODBC Connection pooling and I use SQLDriverConnect very often.

    Thank you,


    Wednesday, June 25, 2014 11:54 AM

All replies

  • Hello,

    The SQL_ATTR_LOGIN_TIMEOUT attribute of the SQLSetConnectAttr() function specify the login timeout period before the connection to the data source completed. You can speicfy the login timeout before call SQLDriverConnect to connect to an SQL Server instance.
    For example,
    // Set login timeout to 5 seconds
    SQLSetConnectAttr(hdbc, SQL_ATTR_LOGIN_TIMEOUT, (SQLPOINTER)5, 0);

    Reference:SQLSetConnectAttr Function

    Fanny Liu

    If you have any feedback on our support, please click here. 

    Fanny Liu
    TechNet Community Support

    Friday, June 27, 2014 7:09 AM
  • Hi Fanny Liu,

    Thank you for your answer, but as this link says SQLSetConnectAttr : The SQL Server Native Client ODBC driver ignores the setting of SQL_ATTR_CONNECTION_TIMEOUT. The link you suggest is from the ODBC standard, and the link I propose is from the Native Client implementation.

    I can say that this timeout of 21 secs I take, is happenning even when I try to connect via TCP to port 1433 when the server is down. It seems like this is a TCP problem. I cannot have my application wait for so long.



    • Edited by Dimitris V Saturday, June 28, 2014 4:06 PM
    Saturday, June 28, 2014 4:03 PM
  • Is right this answer?

    Why pass 0 as size of SQLPOINTER?

    Would be better something like this?:

        SQLUINTEGER uIntVal = 5;
        SQLSetConnectAttr(m_hDBC, SQL_ATTR_LOGIN_TIMEOUT, static_cast<SQLPOINTER>(&uIntVal), static_cast<SQLINTEGER>(sizeof(uIntVal)));

    (BTW, static_cast does not matter here, but I think passing 0 as size of the parameter will end in doing nothing with the passed parameter).

    MVP Visual C++ - Visita mi blog sobre desarrollo:

    Wednesday, October 12, 2016 11:15 AM
  • That's not right. SQLPOINTER is "real" pointer only in case it references string or binary array attribute value. In case of an integer value this parameter holds the attribute value, but not pointer to it. 
    Thursday, November 30, 2017 8:40 AM
  • Very late for this, but there is indeed a registry key for this called LoginTimout.

    For my configuration (Access 2007 running on Windows 10 64 bit) it is located here:

    Computer\HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Office\12.0\Access Connectivity Engine\Engines\ODBC

    Tuesday, August 28, 2018 3:16 PM