这篇文章主要介绍了doctrine实现自动重连mysql数据库机制,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
不知道大家有没有碰到就是mysql有的时候会八小时不使用的话自动断开连接,这样会导致我们的请求失败,项目访问报错,数据库断开,这个时间要是失效了,那我们该怎么办呢?我们使用的是doctrine-dbal,所以那我们就写一套自动重连的机制吧!话不多bb,直接上代码。 <br>
<?php namespace WsdServer\Lib\PDO; use Doctrine\Common\EventManager; use Doctrine\DBAL\Driver; use Doctrine\DBAL\Configuration; use Doctrine\DBAL\DBALException; use Doctrine\DBAL\Cache\QueryCacheProfile; use Doctrine\DBAL\Connection AS Connection; /** * A wrapper around a Doctrine\DBAL\Connection that adds features like * reconnect * */ class WsdConnection extends Connection { const RECONNECT_MAX_TIMES = 3; // 最多重试次数 private $reconnectRetryTimes; public function __construct(array $params, Driver $driver, Configuration $config = null, EventManager $eventManager = null) { parent::__construct($params, $driver, $config, $eventManager); } /** * executeQuery - 支持自动重连机制的封装 * * Executes an, optionally parametrized, SQL query. * * If the query is parametrized, a prepared statement is used. * If an SQLLogger is configured, the execution is logged. * * @param string $query The SQL query to execute. * @param array $params The parameters to bind to the query, if any. * @param array $types The types the previous parameters are in. * @param \Doctrine\DBAL\Cache\QueryCacheProfile|null $qcp The query cache profile, optional. * * @return \Doctrine\DBAL\Driver\Statement The executed statement. * * @throws DBALException * @throws \Exception */ public function executeQuery($query, array $params = array(), $types = array(), QueryCacheProfile $qcp = null) { try { $result = parent::executeQuery($query, $params, $types, $qcp); $this->reconnectRetryTimes = 0; return $result; } catch (DBALException $dex){ if ( $dex->getErrorCode() == 2006 ) { if ($this->reconnectRetryTimes <= WsdConnection::RECONNECT_MAX_TIMES) { $this->reconnectRetryTimes++; secho("ORM-executeQuery", "MySQL Reconnect...(" . $this->reconnectRetryTimes . "/" . WsdConnection::RECONNECT_MAX_TIMES . ")"); $this->close(); return $this->executeQuery($query, $params, $types, $qcp); } } throw $dex; } catch (\Exception $ex) { throw $ex; } } /** * executeUpdate - 支持自动重连机制的封装 * * Executes an SQL INSERT/UPDATE/DELETE query with the given parameters * and returns the number of affected rows. * * This method supports PDO binding types as well as DBAL mapping types. * * @param string $query The SQL query. * @param array $params The query parameters. * @param array $types The parameter types. * * @return integer The number of affected rows. * * @throws DBALException * @throws \Exception */ public function executeUpdate($query, array $params = array(), array $types = array()) { try { $result = parent::executeUpdate($query, $params, $types); $this->reconnectRetryTimes = 0; return $result; } catch (DBALException $dex){ if ( $dex->getErrorCode() == 2006 ) { $this->reconnectRetryTimes++; if ($this->reconnectRetryTimes <= WsdConnection::RECONNECT_MAX_TIMES) { secho("ORM-executeQuery", "MySQL Reconnect...(" . $this->reconnectRetryTimes . "/" . WsdConnection::RECONNECT_MAX_TIMES . ")"); $this->close(); parent::executeUpdate($query, $params, $types); } } throw $dex; } catch (\Exception $ex) { throw $ex; } } }
这样用现在的两个方法覆盖了它原来本身的,这样从连机制简简单单的好了,
测试了一波,发现我们 kill mysql,在重新起来的时候他会自动重新连接,原因是它本身的底层有一个,$this->connect()。所以我们不用怕了
以上是doctrine实现自动重连mysql数据库机制的详细内容。更多信息请关注PHP中文网其他相关文章!

PHP仍然流行的原因是其易用性、灵活性和强大的生态系统。1)易用性和简单语法使其成为初学者的首选。2)与web开发紧密结合,处理HTTP请求和数据库交互出色。3)庞大的生态系统提供了丰富的工具和库。4)活跃的社区和开源性质使其适应新需求和技术趋势。

PHP和Python都是高层次的编程语言,广泛应用于Web开发、数据处理和自动化任务。1.PHP常用于构建动态网站和内容管理系统,而Python常用于构建Web框架和数据科学。2.PHP使用echo输出内容,Python使用print。3.两者都支持面向对象编程,但语法和关键字不同。4.PHP支持弱类型转换,Python则更严格。5.PHP性能优化包括使用OPcache和异步编程,Python则使用cProfile和异步编程。

PHP主要是过程式编程,但也支持面向对象编程(OOP);Python支持多种范式,包括OOP、函数式和过程式编程。PHP适合web开发,Python适用于多种应用,如数据分析和机器学习。

PHP起源于1994年,由RasmusLerdorf开发,最初用于跟踪网站访问者,逐渐演变为服务器端脚本语言,广泛应用于网页开发。Python由GuidovanRossum于1980年代末开发,1991年首次发布,强调代码可读性和简洁性,适用于科学计算、数据分析等领域。

PHP适合网页开发和快速原型开发,Python适用于数据科学和机器学习。1.PHP用于动态网页开发,语法简单,适合快速开发。2.Python语法简洁,适用于多领域,库生态系统强大。

PHP在现代化进程中仍然重要,因为它支持大量网站和应用,并通过框架适应开发需求。1.PHP7提升了性能并引入了新功能。2.现代框架如Laravel、Symfony和CodeIgniter简化开发,提高代码质量。3.性能优化和最佳实践进一步提升应用效率。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP类型提示提升代码质量和可读性。1)标量类型提示:自PHP7.0起,允许在函数参数中指定基本数据类型,如int、float等。2)返回类型提示:确保函数返回值类型的一致性。3)联合类型提示:自PHP8.0起,允许在函数参数或返回值中指定多个类型。4)可空类型提示:允许包含null值,处理可能返回空值的函数。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

Atom编辑器mac版下载
最流行的的开源编辑器

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境