Wie kann ich diese Ausnahme erfassen?
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Duplicate entry '22-85' for key 'ID_CONTACT'
Ich benutze den Frühling, also lösen wir ihn durch org.springframework.dao.DataIntegrityViolationException
try {
ao_history_repository.save(new AoHistory(..));
}
catch (DataIntegrityViolationException e) {
System.out.println("history already exist");
}
catch SQLIntegrityConstraintViolationException , wenn Sie Java 1.6 oder höher verwenden
z.B.
try {
ps.executeUpdate("INSERT INTO ...");
} catch (SQLIntegrityConstraintViolationException e) {
// Duplicate entry
} catch (SQLException e) {
// Other SQL Exception
}
oder
try {
ps.executeUpdate("INSERT INTO ...");
} catch (SQLException e) {
if (e instanceof SQLIntegrityConstraintViolationException) {
// Duplicate entry
} else {
// Other SQL Exception
}
}
vendorCode 2601
wird für unique index constraint
verletzt, sodass Sie SQLException cewndorCode mit e.getErrorCode() == 2601
überprüfen können. Beispielcode:
try {
ao_history_repository.save(new AoHistory(..));
} catch (SQLException e) {
if (e.getErrorCode() == 2601) {
System.out.println("handle duplicate index error here!");
} else {
System.out.println("handle other error code here!");
}
}
Hier ist, was ich benutze, um SQL Exceptions zu protokollieren, damit ich sicher bin, was ich fangen kann.
private static void handleSQLError(SQLException e) throws SQLException {
log.info("SQL Error Type : " + e.getClass().getName());
log.info("Error Message : " + e.getMessage());
log.info("Error Code : " + e.getErrorCode());
log.info("SQL State : " + e.getSQLState());
throw e;
}
Hier ist die Beispielausgabe;
2018 Nis 05 11:20:32,248 INFO MySQLUtil: SQL Error Type : com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException
2018 Nis 05 11:20:32,248 INFO MySQLUtil: Error Message : Duplicate entry 'test2 CAMT052' for key 'customer_file_customer_file_name_idxu'
2018 Nis 05 11:20:32,249 INFO MySQLUtil: Error Code : 1062
2018 Nis 05 11:20:32,249 INFO MySQLUtil: SQL State : 23000
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'test' for key 'customer_file_customer_file_name_idxu'
at Sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at Sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.Java:62)
at Sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.Java:45)
at Java.lang.reflect.Constructor.newInstance(Constructor.Java:422)
Um doppelte Schlüsselausnahmen abzufangen, müssen wir die bestimmte Klasse abfangen, die MySQLIntegrityConstraintViolationException
ist. Der folgende Parameter muss ebenfalls übereinstimmen.
SQL State : 23000
Also benutze ich den folgenden Block, um doppelte Einträge zu fangen;
try {
dbUtil.persistEntry(entry);
} catch (SQLException e) {
if(e instanceof MySQLIntegrityConstraintViolationException) {
if(e.getSQLState().equals("23000")) {
if(e.getMessage().contains("Duplicate")) {
isDuplicateEntryError = true;
}
}
}
}
Siehe Spring Framework Quellcode Schauen Sie sich Spring JDBC Error Resolution Code an.
org.springframework.jdbc.support.SQLErrorCodeSQLExceptionTranslator # doTranslate
else if (Arrays.binarySearch(this.sqlErrorCodes.getDuplicateKeyCodes(), errorCode) >= 0)
{ logTranslation(task, sql, sqlEx, false); return new DuplicateKeyException(buildMessage(task, sql, sqlEx), sqlEx); }
Es gibt mehrere Möglichkeiten, wie Sie verschiedene Exception-Übersetzer treffen können:
Das Verhalten von Dublicate kann sich also von DuplicateKeyException in DataIntegrityViolationException ändern.
Ich benutze Spring ..__
try{
...
} catch (DuplicateKeyException dke) {
...
}
Ich stimme zu, aber wir haben so etwas in meiner Frühlingsanwendung: - RequestValidationException/MessageConstants sind benutzerdefinierte:
import org.springframework.dao.DuplicateKeyException;
|
|
|
catch (Exception exception) {
if(exception instanceof DuplicateKeyException) {
logger.error("exception as duplicate Name Found: " + exception.getMessage());
throw new RequestValidationException(MessageConstants.DUPLICATE_NAME_FOUND_ERROR_CD, MessageConstants.DUPLICATE_NAME_FOUND_ERROR_MSG);
}else{
logger.error("exception on update: " + exception.getMessage());
throw exception;
}
}
Der folgende Code funktioniert für mich:
try {
requete.executeUpdate();
} catch (final ConstraintViolationException e) {
}