laravel-oci8 icon indicating copy to clipboard operation
laravel-oci8 copied to clipboard

How to check wheather database is down or not

Open AviRns opened this issue 7 years ago • 4 comments

Hi Yajra,

Many thanks for all your supports. in case my database is down for maintenance, do you have some method that checks it.

or Refer to Oci8.php line 466, can we return some values if the database didnot connect instead of just throwing Oci8Exception?

Please help

Refer to Oci8.php line 466

private function connect($dsn, $username, $password, array $options, $charset)
    {
        if (array_key_exists(PDO::ATTR_PERSISTENT, $options) && $options[PDO::ATTR_PERSISTENT]) {
            $this->dbh = @oci_pconnect($username, $password, $dsn, $charset);
        } else {
            $this->dbh = @oci_connect($username, $password, $dsn, $charset);
        }

        if (! $this->dbh) {
            $e = oci_error();
            throw new Oci8Exception($e['message']);
        }
    }

AviRns avatar May 30 '17 08:05 AviRns

@AviRns good point on how to check database maintenance. I think we can throw some custom error exception that you can catch via Laravel's error handler.

Maybe something like:

if (! $this->dbh) {
    $e = oci_error();

    if ($e['code'] == 'Maintenance Error Code') {
    	throw new MaintenanceException($e['message'], 0, $e);
    }

    throw new Oci8Exception($e['message']);
}

You can then catch this exception on your Laravel handler.

    public function render($request, Exception $exception)
    {
        if ($exception instanceof MaintenanceException) {
            return 'something friendly?';
        }

        return parent::render($request, $exception);
    }

Just an idea and will try to play with this when I got the chance. Or if you can, please do not hesitate to submit a PR. Thanks!

yajra avatar May 31 '17 01:05 yajra

Maybe, we should also do a try catch instead of suppressing the error?

yajra avatar May 31 '17 01:05 yajra

@yajra Try Catch +1

ChaosPower avatar Jun 20 '17 05:06 ChaosPower

@yajra +1 on custom error handling for DB connection.

Would it be possible to provide a configuration option that would enable more exception handling for this specifically? I don't just mean for maintenance exceptions... for example, if the database connection fails for any reason, I would like to do the following:

(1) redirect the user to an error page (or at least display a custom message) (2) log certain values (which will likely vary based on the exception(s) being thrown) (3) email or otherwise alert the team to the DB failure

This is much easier to achieve if I could--for example--set a custom Exception object to check for in the Laravel Exception Handler render() function. Something like this:

    $e = oci_error();

    if (!empty($this->customExceptionClass)) {
    	throw new $this->customExceptionClass($e['message'], 0, $e);
    }

    throw new Oci8Exception($e['message']);
}

The benefit of a custom exception class is that that there would be more flexibility in handling errors without changing the current error/debugging experience for developers (so long as they don't change their config settings).

Alternatively, if not a customExceptionClass in the configuration, could there be a config option that would take an array of errorCode => exceptionClass values?

blackdrago avatar Jul 21 '17 16:07 blackdrago