-->

Catch DB down exception in playframework

2019-05-25 08:57发布

问题:

I want to handle DB errors when DB are down or not exists,to catch this error in order to make application not crashing and make application keep running even the DB down, error raise when DB is down:

[error] c.j.b.h.AbstractConnectionHook - Failed to acquire connection to jdbc:sqlite:db/dev.db Sleeping for 1000ms and trying again. Attempts left: 4. Exception: null.Message:path to 'db/dev.db': '/home/madian/workspace/mom/src/mom/db' does not exist

回答1:

That Error you can handle in Global.java file in app folder

put a file which Global.java in app folder like this

Global.java file

import play.Application;
import play.GlobalSettings;
import play.libs.F.Promise;
import play.mvc.Result;
import play.mvc.Http.RequestHeader;


public class Global extends GlobalSettings {

    @Override
    public void onStart(Application arg0) {
        // TODO Auto-generated method stub
        super.onStart(arg0);
    }

    @Override
    public void onStop(Application arg0) {
        // TODO Auto-generated method stub
        super.onStop(arg0);
    }
    @Override
    public Promise<Result> onBadRequest(RequestHeader arg0, String arg1) {
        // TODO Auto-generated method stub
        return super.onBadRequest(arg0, arg1);
    }

    @Override
    public Promise<Result> onError(RequestHeader arg0, Throwable arg1) {
        // TODO Auto-generated method stub
        return super.onError(arg0, arg1);
    }


}

in onError method you will receive the Throwable object from the application and can handle it.