Palantir
Palantir

Reputation: 24182

Laravel: setting a MSSQL timeout

I am working on Linux with a MSSQL database on a different host, which may or may not be available. Is it possible to set up a reasonable timeout for this? E.g. 2 or 3 seconds would be more than enough. At the moment, with my default settings, the timeout is over 1 minute.

Connection settings:

    'mssql' => [
      'driver' => 'sqlsrv',
      'host' => 'CDBSQLSERVER',
      'database' => 'MyDatabase',
      'username' => 'XXXX',
      'password' => 'XXXX',
      'charset' => 'utf8',
      'prefix' => ''
    ]

freetds.conf

    [CDBSQLSERVER]
    host = 192.168.43.141
    port = 1433
    tds version = 7.0

I am using it like this:

$myDb = DB::connection('mssql');
$myDb->select(...);

Upvotes: 2

Views: 11647

Answers (2)

Palantir
Palantir

Reputation: 24182

I overlooked the timeouts in the /ets/freetds/freetds.conf! They were commented out.

    [global]
    ...
    timeout = 5
    connect timeout = 5

Will do the trick!

Upvotes: 0

Alexey Mezenin
Alexey Mezenin

Reputation: 163748

Try to use PDO::ATTR_TIMEOUT option:

PDO::ATTR_TIMEOUT: Specifies the timeout duration in seconds. Not all drivers support this option, and its meaning may differ from driver to driver. For example, sqlite will wait for up to this time value before giving up on obtaining an writable lock, but other drivers may interpret this as a connect or a read timeout interval. Requires int.

http://php.net/manual/en/pdo.setattribute.php

In the database.php:

'mysql' => [
    'driver'    => 'mysql',
    'host'      => 'localhost',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
    'options'   => [
        PDO::ATTR_TIMEOUT => 1,
    ],
],

Upvotes: 6

Related Questions