I want to check if we can't connect to the database with symfony 6.2.6.
I'm using this code:
$conn = $doctrine->getConnection($dbName);
try {
$conn->connect();
} catch(Exception $e) {
return "Failed to use database: " $e->getMessage();
}
if(!$conn->isConnected()) {
return "Could not connect to database";
}
return "Fine !";
I have this in my doctrine.yaml
:
doctrine:
dbal:
connections:
default:
url: '%env(DATABASE_URL)%'
mapping_types:
bit: boolean
options:
timeout: 5
I well get the error like Connection timed out
. BUT, the problem is that it takes lot of times to timed out. I set timeout: 5
to do it but it doesn't seems to change anything, it already wait 30s before timing out.
How can I make the connection failed after 5 seconds instead of 30?
The options are the same as passing flags to the driver. The answer here shows the values you pass for PDO or MySQLi, however you should be able to use a constant value as a key rather than using the constant's numeric value directly.
For MySQLi:
doctrine:
dbal:
connections:
default:
options:
- !php/const 'MYSQLI_OPT_CONNECT_TIMEOUT': 5
For PDO:
doctrine:
dbal:
connections:
default:
options:
- !php/const 'PDO::ATTR_TIMEOUT': 5