arntech / doctrine-timeout-handler
ARNTech Doctrine 超时处理。
v0.0.2
2020-08-05 09:57 UTC
Requires
- php: ^5.6 || ^7.0
- facile-it/doctrine-mysql-come-back: *
- psr/log: *
This package is auto-updated.
Last update: 2024-09-05 19:07:17 UTC
README
这是什么?
这是一个尝试处理数据库连接超时以及 "MySQL 服务器已断开(错误 2006)" 错误的库。
安装
推荐通过 [Composer][] 安装。运行以下命令安装包并将其添加到项目 composer.json
的依赖中。
composer require arntech/doctrine-timeout-handler
依赖
该库使用 [psr/log][] 和 [facile-it/doctrine-mysql-come-back][]。
配置
与 facile-it 解决方案类似(快速迁移)。
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' => 'ARNTech\DoctrineTimeout\Connection', 'driverClass' => 'ARNTech\DoctrineTimeout\Driver\PDOMySql\Driver', 'driverOptions' => array( 'x_reconnect_attempts' => 3, 'check_connection_beforehand' => true //this specifies to check if connection is still alive before executing anything ) ); $conn = DriverManager::getConnection($connectionParams, $config);
在 Symfony 2 项目上的 yaml 配置示例
# Doctrine example Configuration doctrine: dbal: default_connection: %connection_name% connections: %connection_name%: host: %database_host% port: %database_port% dbname: %database_name% user: %database_user% password: %database_password% charset: UTF8 wrapper_class: 'ARNTech\DoctrineTimeout\Connection' driver_class: 'ARNTech\DoctrineTimeout\Driver\PDOMySql\Driver' options: x_reconnect_attempts: 3 check_connection_beforehand: true #this specifies to check if connection is still alive before executing anything
如果您正在使用 DSN/database_url
环境变量设置数据库连接(如 Doctrine Symfony Flex 食谱建议),则必须从数据库 URL 中移除协议。否则,Doctrine 会忽略您的 driver_class
配置并使用默认协议驱动程序,这会导致错误。
doctrine: dbal: connections: default: # DATABASE_URL needs to be without driver protocol. # use "//db_user:db_password@127.0.0.1:3306/db_name" # instead of "mysql://db_user:db_password@127.0.0.1:3306/db_name" url: '%env(resolve:DATABASE_URL)%' wrapper_class: 'ARNTech\DoctrineTimeout\Connection' driver_class: 'ARNTech\DoctrineTimeout\Driver\PDOMySql\Driver' options: x_reconnect_attempts: 3 check_connection_beforehand: true #this specifies to check if connection is still alive before executing anything
在 Zend Framework 2/3 项目上的配置示例
return [ 'doctrine' => [ 'connection' => [ 'orm_default' => [ 'driverClass' => \ARNTech\DoctrineTimeout\Driver\PDOMySql\Driver::class, 'wrapperClass' => \ARNTech\DoctrineTimeout\Connection::class, 'params' => [ 'host' => 'localhost', 'port' => '3307', 'user' => '##user##', 'password' => '##password##', 'dbname' => '##database##', 'charset' => 'UTF8', 'driverOptions' => [ 'x_reconnect_attempts' => 9, 'check_connection_beforehand' => true //this specifies to check if connection is still alive before executing anything ] ], ], ], ], ];