Catch DB down exception in playframework

[亡魂溺海] 提交于 2019-12-22 13:11:13

问题


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.



来源:https://stackoverflow.com/questions/27297283/catch-db-down-exception-in-playframework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!