doctrine-mysql-come-back
doctrine-mysql-come-back copied to clipboard
Doctrine DBAL able to try reconnect on MySQL has gone away exceptions
DoctrineMySQLComeBack
Auto reconnect on Doctrine MySql has gone away exceptions on doctrine/dbal
.
Installation
If you're using DBAL 3
$ composer require facile-it/doctrine-mysql-come-back ^2.0
If you're using DBAL ^2.3
$ composer require facile-it/doctrine-mysql-come-back ^1.0
Configuration
In order to use DoctrineMySQLComeBack you have to set the wrapperClass
connection parameter.
You can choose how many times Doctrine should be able to reconnect, setting x_reconnect_attempts
driver option. Its value should be an int.
If you're using DBAL v2, you also need to set the driverClass
parameter too; please refer to the previous version of this readme for that.
An example of configuration at connection instantiation time:
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
$config = new Configuration();
//..
$connectionParams = array(
'dbname' => 'mydb',
'user' => 'user',
'password' => 'secret',
'host' => 'localhost',
// [doctrine-mysql-come-back] settings
'wrapperClass' => 'Facile\DoctrineMySQLComeBack\Doctrine\DBAL\Connection',
'driverClass' => 'Facile\DoctrineMySQLComeBack\Doctrine\DBAL\Driver\PDO\MySQL\Driver',
'driverOptions' => array(
'x_reconnect_attempts' => 3
)
);
$conn = DriverManager::getConnection($connectionParams, $config);
//..
An example of yaml configuration on Symfony projects:
doctrine:
dbal:
connections:
default:
# DATABASE_URL would be of "mysql://db_user:[email protected]:3306/db_name"
url: '%env(resolve:DATABASE_URL)%'
wrapper_class: 'Facile\DoctrineMySQLComeBack\Doctrine\DBAL\Connection'
options:
x_reconnect_attempts: 3
An example of configuration on Laminas Framework 2projects:
return [
'doctrine' => [
'connection' => [
'orm_default' => [
'wrapperClass' => \Facile\DoctrineMySQLComeBack\Doctrine\DBAL\Connection::class,
'params' => [
'host' => 'localhost',
'port' => '3307',
'user' => '##user##',
'password' => '##password##',
'dbname' => '##database##',
'charset' => 'UTF8',
'driverOptions' => [
'x_reconnect_attempts' => 9,
]
],
],
],
],
];
You can use wrapper class Facile\DoctrineMySQLComeBack\Doctrine\DBAL\Connections\MasterSlaveConnection
if you are using master / slave Doctrine configuration.
Usage
Since DBAL v3, Connection::refresh
does not exist anymore, so you don't need to do anything else to leverage the reconnection, it will be automagically done.
From v1.6
of this library automagically reconnection is enabled also during $em->getConnection()->beginTransaction()
calls,
and this works also during simple $em->flush()
, if out of a previous transaction.
Thanks
Thanks to Dieter Peeters and his proposal on DBAL-275. Check it out if you are using doctrine/dbal <2.3.