Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to check wheather database is down or not #296

Open
AviRns opened this issue May 30, 2017 · 4 comments
Open

How to check wheather database is down or not #296

AviRns opened this issue May 30, 2017 · 4 comments

Comments

@AviRns
Copy link

AviRns commented May 30, 2017

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']);
        }
    }
@yajra
Copy link
Owner

yajra commented May 31, 2017

@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
Copy link
Owner

yajra commented May 31, 2017

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

@ChaosPower
Copy link
Collaborator

@yajra Try Catch +1

@blackdrago
Copy link

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants