facile-it / doctrine-mysql-come-back
Auto reconnect on Doctrine MySql has gone away exceptions on doctrine/dbal
Installs: 2 300 706
Dependents: 4
Suggesters: 0
Security: 0
Stars: 198
Watchers: 39
Forks: 40
Open Issues: 5
Requires
- php: ^8.1
- doctrine/dbal: ^4.0.0
Requires (Dev)
- ext-pdo: *
- facile-it/facile-coding-standard: ^0.5.2
- infection/infection: ^0.27.10
- phpdocumentor/reflection-docblock: >=4.0.1
- phpspec/prophecy-phpunit: ^2.0
- phpunit/phpunit: ^10.5
- psalm/plugin-phpunit: ^0.18.4
- rector/rector: ^1.0
- vimeo/psalm: ^5.8.0
- dev-master
- 3.x-dev
- 3.0.0
- 2.x-dev
- 2.0.0
- 2.0.0-BETA4
- 2.0.0-BETA3
- 2.0.0-BETA2
- 2.0.0-BETA1
- 1.10.1
- 1.10.0
- v1.10.0-beta1
- 1.9.x-dev
- v1.9.0
- 1.8.x-dev
- v1.8
- v1.7
- v1.6.5
- v1.6.4
- v1.6.3
- v1.6.2
- v1.6.1
- v1.6
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5
- v1.4
- v1.3
- v1.2
- dev-dependabot/github_actions/codecov/codecov-action-5
- dev-hotfix-final-class
- dev-interface-consitency
- dev-full-statement-support
- dev-hotfix-ci-lowest-deps
This package is auto-updated.
Last update: 2024-11-18 17:05:34 UTC
README
DoctrineMySQLComeBack
This library tries to solve the infamous "MySQL has gone away" issue, and similar ones.
It does so by providing a doctrine/dbal
driver wrapper that automatically reconnects to the database server when applicable; to avoid consistency issues, the reconnection is not attempted when writes are concerned (i.e. open transaction, timeout on write queries).
Installation
If you're using DBAL 4.0+
$ composer require facile-it/doctrine-mysql-come-back ^3.0
If you're using DBAL 3.6+
$ 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 must 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 = [ 'dbname' => 'mydb', 'user' => 'user', 'password' => 'secret', 'host' => 'localhost', // [doctrine-mysql-come-back] settings 'wrapperClass' => 'Facile\DoctrineMySQLComeBack\Doctrine\DBAL\Connection', 'driverOptions' => [ '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:db_password@127.0.0.1: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\PrimaryReadReplicaConnection
if you are using a primary/replica Doctrine configuration:
use Doctrine\DBAL\Configuration; use Doctrine\DBAL\DriverManager; $config = new Configuration(); //.. $connectionParams = [ 'wrapperClass' => 'Facile\DoctrineMySQLComeBack\Doctrine\DBAL\PrimaryReadReplicaConnection', 'primary' => [ // ... 'driverOptions' => [ 'x_reconnect_attempts' => 3 ], ], ]; $conn = DriverManager::getConnection($connectionParams, $config); //..
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.