问题
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