locked
How to set a timeout for a sqlsrv_connect() ? RRS feed

  • Question

  • I have in my php.ini:

     

    ;extension=php_sqlsrv.dll
    ;extension=php_sqlsrv_ts.dll
    extension=php_sqlsrv_52_ts_vc6.dll

     

    And I'm using IIS 6 on Windows Server 2003.

    I'm trying to define a timeout for a sqlsrv_connect() call, but it seems there's no "sqlsrv.connect_timeout" directive that I can set via ini_set(). The script below prints nothing:

     

    <?

    print ini_get('sqlsrv.connect_timeout') ;

    ?>

     

    Thanks for any tips! :)

     

    Wednesday, July 28, 2010 6:49 PM

Answers

  • The connection timeout is set in a connection options array when  you create the connection. Take a look at the LoginTimeout option here: Connection Options. To see how the connection options array is used, look at the example in this topic: sqlsrv_connect.

    Let us know if that's helpful/not helpful.

    Thanks.

    -Brian


    This posting is provided "AS IS" with no warranties, and confers no rights. http://blogs.msdn.com/brian_swan
    Wednesday, July 28, 2010 8:09 PM

All replies

  • The connection timeout is set in a connection options array when  you create the connection. Take a look at the LoginTimeout option here: Connection Options. To see how the connection options array is used, look at the example in this topic: sqlsrv_connect.

    Let us know if that's helpful/not helpful.

    Thanks.

    -Brian


    This posting is provided "AS IS" with no warranties, and confers no rights. http://blogs.msdn.com/brian_swan
    Wednesday, July 28, 2010 8:09 PM
  • sqlsrv_connect seems to ignore the LoginTimeout option.

    If I use an incorrect "Database" value in the array , even with the timeout set to 5 or 10, it will still take about 50 seconds to give up connecting.

    Thanks,

    Wednesday, April 6, 2011 8:32 PM
  • Jens_msdn-

    Just to be clear, if you execute this...

    $conn = sqlsrv_connect($host, array("UID"=>$user, "PWD"=>$pwd, "Database"=>"wrongDBName", "LoginTimeout"=>5));

    ...about 50 seconds elapse before the connection fails?

    But this...

    $conn = sqlsrv_connect($host, array("UID"=>$user, "PWD"=>$pwd, "Database"=>"correctDBName", "LoginTimeout"=>5));

    ...connects right away?

    -Brian


    This posting is provided "AS IS" with no warranties, and confers no rights. http://blogs.msdn.com/brian_swan
    Wednesday, April 6, 2011 9:47 PM
  • Hi! Maybe I should have created another thread and I will if I don't get a response, but I'm having the exact same problem, with one variation - it's the host that's causing it to hang indefinitely.  To be clear, if I execute this...

    $conn = sqlsrv_connect($WRONG_HOST, array("UID"=>$user, "PWD"=>$pwd, "Database"=>$db, "LoginTimeout"=>5));

    ...execution seems to drop dead in its tracks and I get this error:

    Fatal error: Maximum execution time of 30 seconds exceeded in C:\webs\inetpub\includes\class\database.php on line 64

    But if I execute this...

    $conn = sqlsrv_connect($RIGHT_HOST, array("UID"=>$user, "PWD"=>$pwd, "Database"=>$db, "LoginTimeout"=>5));

    ...it works fine! I assume this is because the default of LoginTimeout is "No timeout" and it's ignoring my setting. I actually have a weird requirement where I need to be able to literally provide different hosts within the app, so I have to be able to detect if a provided host fails. Any ideas why this is happening?

    Monday, December 10, 2012 8:55 AM
  • The option works.

    It depends what you are doing in your PHP code after sqlsrv_connect fails.  Something else is taking a long time after your call to sqlsrv_connect.  It isn't the timeout option not working.


    Rob

    Monday, December 10, 2012 12:27 PM
  • I'm pretty confident it's the sqlsrv_connect call that's hanging execution.  I can go like this: 

    echo "first here";

    $conn = sqlsrv_connect($WRONG_HOST, array("UID"=>$user, "PWD"=>$pwd, "Database"=>$db, "LoginTimeout"=>5)); echo "second here"; exit;


    And it still times out after 30 seconds without ever printing the "second here" echo statement.


    Tuesday, December 11, 2012 5:11 PM
  • Sorry, but there's not much more I can add. 

    Check your php log file, and your Windows event log, and try the exact same settings to connect using SQL Server Management Studio.


    Rob

    Wednesday, December 12, 2012 10:28 AM