private static final ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
private final Class<?> psqlExceptionClass;
private final Method getServerErrorMessageMethod;
private final Method getConstraintMethod;
{
Class<?> psqlExceptionClass;
Method getServerErrorMessageMethod;
Method getConstraintMethod;
try {
Class<?> clazz = Class.forName("org.postgresql.util.PSQLException");
psqlExceptionClass = clazz;
getServerErrorMessageMethod = clazz.getDeclaredMethod("getServerErrorMessage");
getConstraintMethod = getServerErrorMessageMethod.getReturnType().getDeclaredMethod("getConstraint");
} catch (Exception ex) {
psqlExceptionClass = null;
getServerErrorMessageMethod = null;
getConstraintMethod = null;
}
this.psqlExceptionClass = psqlExceptionClass;
this.getServerErrorMessageMethod = getServerErrorMessageMethod;
this.getConstraintMethod = getConstraintMethod;
}
@Override
protected String doExtractConstraintName(SQLException sqle) throws NumberFormatException {
final SQLException sqlException = NewJdbcExceptionHelper.extractExceptionUsingSQLState(sqle);
if (psqlExceptionClass != null && psqlExceptionClass.isInstance(sqlException)) {
try {
Object serverErrorMessage = getServerErrorMessageMethod.invoke(sqlException);
if (serverErrorMessage != null) {
return (String) getConstraintMethod.invoke(serverErrorMessage);
}
} catch (Exception e) {
throw new RuntimeException("Something in the PostgreSQL integration went wrong. Please report this issue!", e);
}
}
return null;
}
}