• Stars
    star
    195
  • Rank 194,123 (Top 4 %)
  • Language
    PHP
  • License
    Apache License 2.0
  • Created almost 10 years ago
  • Updated about 1 month ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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 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:[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\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.

More Repositories

1

paraunit

Run PHPUnit tests in parallel
PHP
134
star
2

FunctionalKit

Basic functions and combinators for functional programming in Swift.
Swift
85
star
3

php-openid-client

PHP OpenID Client
PHP
35
star
4

mongodb-bundle

Bundle service integration of official mongodb/mongo-php-library
PHP
35
star
5

rabbitmq-consumer

A configurable RabbitMQ consumer made in Rust, useful for a stable and reliable CLI commands processor.
Rust
27
star
6

terminable-loop-command

A Shell+PHP wrapper to run Symfony console commands in loop under a daemon or Kubernetes
PHP
26
star
7

mutoid

Reactive library for data fetching, caching, state management
TypeScript
25
star
8

sentry-module

This module allows integration of Sentry Client into laminas and mezzio
PHP
18
star
9

facile-it.github.io

Facile.it Engineering Blog
HTML
16
star
10

crossbar-http-publisher-bundle

This bundle allows to submit PubSub events via HTTP/POST requests to a Crossbar HTTP Publisher.
PHP
10
star
11

facile-coding-standard

Repository with all coding standard ruleset used in Facile.it
PHP
10
star
12

moka

Shorthand for creating mock objects β˜•
PHP
9
star
13

php-codec

A partial porting of io-ts in PHP.
PHP
9
star
14

sentry-psr-log

PSR Logger for Sentry
PHP
7
star
15

fortepiano

Playing actual music over fp-ts notes
TypeScript
7
star
16

paraunit-testcase

TestCase and client to test Symfony applications with Doctrine database isolation
PHP
6
star
17

SourceryTemplates

A collection of templates for Sourcery, written in Stencil.
6
star
18

Optics

Lenses and Prisms for Swift
Swift
6
star
19

eslint-config-facile

eslint-config-facile
TypeScript
6
star
20

NavigationHelper

A simple helper to aid navigation in iOS and macOS apps
Swift
4
star
21

paginator-bundle

Symfony2 Paginator Bundle
PHP
4
star
22

NetworkingKit

Basic abstractions for HTTP requests
Swift
4
star
23

json-api-ts

TypeScript
3
star
24

easy-android

A set of common utils functions used within Facile.it Android applications
Kotlin
3
star
25

json-api-php

A JSON API Serializer/Deserializer made in PHP
PHP
3
star
26

zf-link-headers-module

Zend Framework module to push Link headers for resourcers added with HeadLink view helper
PHP
3
star
27

Functional

Basic functions and combinators for functional programming in Swift.
Swift
3
star
28

capistrano-strategy-copy-with-triggers

Capistrano copy-strategy fork that offers some hooks to prepare the build before compression and distribution.
Ruby
3
star
29

DialogContainerController

Swift
2
star
30

doctrine-dynamic-discriminator-map

Doctrine Dynamic Discriminator Map
PHP
2
star
31

hackathon2015

Facile.it Hackathon 2015 website
HTML
2
star
32

php-jose-verifier

JOSE JWT verifiers for OAuth2 and OpenID tokens
PHP
2
star
33

openid-bundle

A Symfony bundle to do authorization through a third-party OpenId provider
PHP
1
star
34

hackathon2017

Website for FacileHack 2017 edition
CSS
1
star
35

xcode-testing-in-pratica

Progetto Xcode associato all'articolo "Xcode Testing in pratica"
Swift
1
star
36

validoo

PHP
1
star
37

mongodb-messenger-transport

A Symfony Messenger transport on MongoDB, on top of facile-it/mongodb-bundle
PHP
1
star
38

php-oauth2-http-client

HTTPlug plugin for OpenID/OAuth2 authorization support
PHP
1
star