No ConstraintName when using PostgreSQL (BatchUpdateException instead of PSQLException)

Description

> When using dialect org.hibernate.dialect.PostgreSQLDialect and a ConstraintViolationException is thrown, getConstraintName() does not get the right constraint name.
> It seems that there is something wrong with the ConstraintNameExtractor.

I still run into this problem.

lets take a look into the PostgresDialect:

------ BOF ------ private static ViolatedConstraintNameExtracter EXTRACTER = new TemplatedViolatedConstraintNameExtracter() {
public String extractConstraintName(SQLException sqle) {
try {
int sqlState = Integer.valueOf( JDBCExceptionHelper.extractSqlState(sqle)).intValue();
switch (sqlState) {
// CHECK VIOLATION
case 23514: return extractUsingTemplate("violates check constraint \"","\"", sqle.getMessage());
// UNIQUE VIOLATION
case 23505: return extractUsingTemplate("violates unique constraint \"","\"", sqle.getMessage());
// FOREIGN KEY VIOLATION
case 23503: return extractUsingTemplate("violates foreign key constraint \"","\"", sqle.getMessage());
// NOT NULL VIOLATION
case 23502: return extractUsingTemplate("null value in column \"","\" violates not-null constraint", sqle.getMessage());
// TODO: RESTRICT VIOLATION
case 23001: return null;
// ALL OTHER
default: return null;
}
} catch (NumberFormatException nfe) {
return null;
}
}
};
------ EOF ------
My problem ist that sqle is a BatchUpdateException and not the suspected PSQLExpection.
BatchUpdateException message doesn't contain the constraintName!

I sloved that by adding
"SQLException psqle = sqle.getNextException();"
and replace the "sqle.getMesage()" with "psqle.getMessage()"

Activity

Steve EbersoleMarch 21, 2011 at 7:05 PM

Bulk closing stale resolved issues

JonathanAJune 19, 2008 at 1:34 AM

This is still an issue it hasn't been resolved. The problem is that when you are doing an update operation and a constraint violation happens then the exception that hibernate tries to pull the constraintName from is the BatchUpdateException which does not contain the root cause exception text. You can see in the JDBCExceptionHelper.extractSqlState(sqle) it does this

<code>
public static String extractSqlState(SQLException sqlException) {
String sqlState = sqlException.getSQLState();
SQLException nested = sqlException.getNextException();
while ( sqlState == null && nested != null ) {
sqlState = nested.getSQLState();
nested = nested.getNextException();
}
return sqlState;
}
</code>

There it calls getNextException to get the correct exception sqlState, but then when the message is processed the wrong one is used.

There should be another method on JDBCExceptionHelper to get the nested SQL state exception and then that should be used to pull the message off of.

Duplicate

Details

Assignee

Reporter

Priority

Created November 6, 2007 at 1:06 AM
Updated March 21, 2011 at 7:05 PM
Resolved November 16, 2007 at 5:45 AM