Identifying SQLException due to network problem

北城以北 提交于 2019-12-11 10:43:25

问题


I am developing a web application which uses 3 different databases(Oracle & MSSQL). This application contains spring and hibernate frameworks. In this application if databases goes down or some network issue comes I have to handle differently(While trying to access database). Currently if the above scenario comes I am getting SQLException as the least cause. How do I identify that SQLException thrown is because of network issue or some query/data issue?


回答1:


I guess you'll just have to extract this information from the exception message. For example in case of Oracle a SQLException exception with following message is thrown:

Io exception: The Network Adapter could not establish the connection



回答2:


Check the drivers' individual documentation and see what error codes are thrown as a result of the errors you described

UPDATE

} catch (SQLException se) {
   int count = 1;
   while (se != null) {
       System.out.println("SQLException " + count);
       System.out.println("Code: " + se.getErrorCode());
       System.out.println("SqlState: " + se.getSQLState());
       System.out.println("Error Message: " + se.getMessage());

       se = se.getNextException();
       count++;
   }
}

each database vendor provides their own error codes and states, if you check them you should be safe.



来源:https://stackoverflow.com/questions/7481076/identifying-sqlexception-due-to-network-problem

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