} else if (e.getCause() != null && e.getCause() instanceof SQLException) {
sqlException = (SQLException) e.getCause();
} else {
// It is not even a SQLException. Something else is wrong, so propagate
// the error.
throw new SnapshotRepositoryRuntimeException(e.getMessage(), e);
}
String sqlState = sqlException.getSQLState();
if (sqlState != null) {
// Look for SQL syntax errors, both ISO style and XOpen style.
if (sqlState.startsWith("42") || sqlState.startsWith("S0")
|| sqlState.startsWith("37")) {
LOG.log(Level.WARNING, "Could not execute SQL query on the database.",
sqlException);
// Swallow the exception.
} else if (sqlState.startsWith("08")) { // Connection errors.
LOG.log(Level.WARNING, "Unable to connect to the database.",
sqlException);
throw new SnapshotRepositoryRuntimeException(
"Unable to connect to the database.", sqlException);
} else {
throw new SnapshotRepositoryRuntimeException(sqlException.getMessage(),
sqlException);
}
} else {
// No SQLState to consider. Check connectivity with DB.
Connection conn = null;
try {
conn = session.getConnection();
LOG.log(Level.WARNING, "Could not execute SQL query on the database.",
e);
// Swallow the exception.
} catch (RuntimeException e1) {
Throwable cause = (e1.getCause() != null &&
e1.getCause() instanceof SQLException) ? e1.getCause() : e1;
LOG.log(Level.WARNING, "Unable to connect to the database", cause);
throw new SnapshotRepositoryRuntimeException(
"Unable to connect to the database.", cause);
} finally {
if (conn != null) {
try {
conn.close();