小熊猫/doctrine-reconnect

Zend Framework 2 模块,当数据库“离线”时尝试重新连接

0.1.1 2015-01-09 22:25 UTC

This package is auto-updated.

Last update: 2024-08-24 04:00:19 UTC


README

一个小型的 Zend Framework 2 模块,当数据库“离线”时尝试重新连接。

Latest Stable Version Total Downloads Latest Unstable Version License

Build Status Code Coverage Scrutinizer Code Quality Dependency Status

为了实现这一点,它覆盖了 DoctrineORMModule 提供的 Doctrine DBAL 驱动程序。

我没有发明这个方法。我只是喜欢它,并认为它值得一个 zf2 模块,这样就可以轻松重用。这个方法的功劳应该归功于 circlical

安装

php composer.phar require bushbaby/doctrine-reconnect

然后,将 BsbDoctrineReconnect 添加到您的 config/application.config.php

调整您的 Doctrine DB 配置

将 config/bsb-doctrine-reconnect.local.php.dist 复制到您的 config/autoload/config/bsb-doctrine-reconnect.local.php 目录中,并覆盖您定义的每个连接的指定 driverClass。

<?php
return array(
    'doctrine' => array(
        'connection' => array(
            'orm_default' => array(
                'driverClass' => 'BsbDoctrineReconnect\DBAL\Driver\PDOMySql\Driver',
                'wrapperClass' => 'BsbDoctrineReconnect\DBAL\Connection',

                'params' => array(
                    'driverOptions' => array(
                        'x_reconnect_attempts' => 10,
                    ),
                )
            )
        )
    ),
);

测试

您可以通过从 cli 连接到 mysql 并终止适当的连接来手动测试这实际上是否有效。

mysql> SHOW PROCESSLIST;
+----+------+-----------+----------+---------+------+-------+------------------+
| Id | User | Host      | db       | Command | Time | State | Info             |
+----+------+-----------+----------+---------+------+-------+------------------+
|  1 | root | localhost | database | Sleep   |    1 |       | NULL             |
|  4 | root | localhost | NULL     | Query   |    0 | NULL  | SHOW PROCESSLIST |
+----+------+-----------+----------+---------+------+-------+------------------+
4 rows in set (0.00 sec)

mysql> KILL 1;
Query OK, 0 rows affected (0.00 sec)

已知限制

这种方法只适用于非事务性语句。