dqvtm82066 2015-04-19 16:33 采纳率: 0%
浏览 30
已采纳

在Symfony2中捕获数据库异常

I've got a random problem that I can't narrow down. Occasionally, I will get the following error in a Symfony2 application:

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

I would like to setup an application-wide listener to catch the PDOException and log some information. How can I hook into Symfony to only catch PDOException?

  • 写回答

2条回答 默认 最新

  • duanfen2349 2015-04-19 18:46
    关注

    You need to create custom exception listener. It will listen to all exceptions, but you will specify type check inside it.

    In your services.yml you need to specify listener:

    kernel.listener.your_pdo_listener:
            class: Acme\AppBundle\EventListener\YourExceptionListener
            tags:
               - { name: kernel.event_listener, event: kernel.exception, method: onPdoException }
    

    Now you need to create this class:

    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
              }
         }
    }
    

    Check doc how to create event listener

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 征集Python提取PDF文字属性的代码
  • ¥15 有偿求苍穹外卖环境配置
  • ¥15 代码在keil5里变成了这样怎么办啊,文件图像也变了,
  • ¥20 Ue4.26打包win64bit报错,如何解决?(语言-c++)
  • ¥15 clousx6整点报时指令怎么写
  • ¥30 远程帮我安装软件及库文件
  • ¥15 关于#自动化#的问题:如何通过电脑控制多相机同步拍照或摄影(相机或者摄影模组数量大于60),并将所有采集的照片或视频以一定编码规则存放至规定电脑文件夹内
  • ¥20 深信服vpn-2050这台设备如何配置才能成功联网?
  • ¥15 Arduino的wifi连接,如何关闭低功耗模式?
  • ¥15 Android studio 无法定位adb是什么问题?