среда, 3 августа 2016 г.

Catching connection exceptions in PHP 7 for Mongodb



When it comes time to prepare your code for release environment instead of dev you would probably need to make sure it will work stable under any circumstances. One of the points of such preparation is to make sure your service will run if MongoDB (in case if you use it for logging only or saving bulk info of course) will go down for some reason.
In PHP 7 we have a new driver for working with Mongo, thus it would need some new approach to handling the exception.
In code it would look like this:


<?php
try {
            //information handling
        
        } catch (\MongoDB\Driver\Exception\ConnectionException $e) {
            //echo "Mongo error";
        }
?>


This would allow your script to continue doing its job even if MongoDB is unavailable for now.

If we had no this code you would get fatal error and script will stop executing.

Luckily, we are safe now and can stop worrying about this problem (however, make sure to log error to log and send yourself an email concerning this problem to solve it quickly)

Комментариев нет:

Отправить комментарий