Philipp Nies
Philipp Nies

Reputation: 969

Check if DB is available

I need to check if a postgres db is available.

If the db is online, I get the timestamp.

If the db is offline, I get a laravel PDO Exception

But every time I get a Exception and the script execution stops

try
{
    $con = new PDO("pgsql:host=".$db->Host.";port=".$db->Port.";dbname=postgres", $db->Username, $db->Password, [PDO::ATTR_TIMEOUT => '5']);

    $dt = new \DateTime(null, new \DateTimeZone('Europe/Berlin'));

    return $dt->getTimestamp();
}
catch(Exception $e)
{
    return false;       
}
catch(PDOException $e)
{
    return false;       
}

Upvotes: 1

Views: 71

Answers (1)

KiwiJuicer
KiwiJuicer

Reputation: 1982

Try to prepend \ infront of the exceptions:

try
{
    $con = new PDO("pgsql:host=".$db->Host.";port=".$db->Port.";dbname=postgres", $db->Username, $db->Password, [PDO::ATTR_TIMEOUT => '5']);

    $dt = new \DateTime(null, new \DateTimeZone('Europe/Berlin'));

    return $dt->getTimestamp();
}
catch(\Exception $e)
{
    return false;       
}

Upvotes: 1

Related Questions