php 捕获数据库异常,php – 在Symfony2中捕获数据库异常

Symfony PDOException 监听

我有一个随机的问题,我无法缩小范围.偶尔,我会在Symfony2应用程序中收到以下错误:

Uncaught Exception: An exception occured in driver: SQLSTATE[08004] [1040] Too many connections {“type”:1,”file”:”/var/www/symfony/vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php”,”line”:115,”level”:30709

我想设置一个应用程序范围的侦听器来捕获PDOException并记录一些信息.如何挂钩到Symfony才能捕获PDOException?

解决方法:

您需要创建自定义异常侦听器.它将侦听所有异常,但您将在其中指定类型检查.

在您的services.yml中,您需要指定监听器:

kernel.listener.your_pdo_listener:

class: Acme\AppBundle\EventListener\YourExceptionListener

tags:

- { name: kernel.event_listener, event: kernel.exception, method: onPdoException }

现在您需要创建此类:

YourExceptionListener:

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;

class YourExceptionListener

{

public function onPdoException(GetResponseForExceptionEvent $event)

{

$exception = $event->getException();

if ($exception instanceof PDOException) {

//now you can do whatever you want with this exception

}

}

}

标签:doctrine-orm,php,symfony,pdo,doctrine

来源: https://codeday.me/bug/20191007/1867126.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值