MyGit
🚩收到GitHub仓库的更新通知

facile-it/doctrine-mysql-come-back

Fork: 40 Star: 195 (更新于 2024-05-14 09:27:13)

license: Apache-2.0

Language: PHP .

Doctrine DBAL able to try reconnect on MySQL has gone away exceptions

最后发布版本: 3.0.0 ( 2024-03-10 19:33:47)

GitHub网址

✨免费申请网站SSL证书,支持多域名和泛域名,点击查看

Latest Stable Version Latest Unstable Version Total Downloads

Build status Test coverage License

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.

最近版本更新:(数据更新于 2024-04-27 14:26:54)

2024-03-10 19:33:47 3.0.0

2023-06-08 20:03:17 2.0.0

2023-04-05 23:31:26 2.0.0-BETA4

2023-04-02 06:51:08 2.0.0-BETA3

2023-04-02 05:33:29 2.0.0-BETA2

2022-06-23 15:24:40 2.0.0-BETA1

2021-04-23 20:31:47 1.10.1

2021-03-26 00:19:05 1.10.0

2021-01-08 00:46:22 v1.10.0-beta1

2020-11-02 23:56:23 v1.9.0

facile-it/doctrine-mysql-come-back同语言 PHP最近更新仓库

2024-05-21 00:26:17 statamic/cms

2024-05-20 01:27:21 elementor/elementor

2024-05-17 23:51:20 symfony/var-dumper

2024-05-17 23:50:58 symfony/routing

2024-05-17 23:48:06 symfony/symfony

2024-05-16 06:01:21 PHP-CS-Fixer/PHP-CS-Fixer