I have a database table which has both Foreign key and Unique key constraints. To make changes in the database table, i am using java application which uses hibernate. To be more precise i am doing Session.save() to make changes in database table. My code looks like this.
try{
transaction.begin();
------logic-------
session.save();
transaction.commit();
}catch(ConstarintViolationException exception){
---here is problem------
}
In case of any type constraint violation hibernate throws the same exception named ConstraintViolationException.
My question is what should i do to identify that what constraint has caused the exception. I tried doing
exception.getConstraintName()
But unfortunately i am getting null as output.
I want to differentiate between the two cases because i want to translate the hibernate exception to my own exception before sending it to client.
I would say let the database detect the violations of the constraint.....
Or
You can have something like the below class which might help in your case to catch the constraint violation exceptions...
public class DataIntegrityViolationExceptionsAdvice {
public void afterThrowing(DataIntegrityViolationException ex) throws DataIntegrityViolationException {
// extract the affected database constraint name:
String constraintName = null;
if ((ex.getCause() != null) && (ex.getCause() instanceof ConstraintViolationException)) {
constraintName = ((ConstraintViolationException) ex.getCause()).getConstraintName();
}
// create a detailed message from the constraint name if possible
String message = ConstraintMsgKeyMappingResolver.map(constraintName);
if (message != null) {
throw new DetailedConstraintViolationException(message, ex);
}
throw ex;
}
}
Related
I've a JPA Entity which has a unique constraint based on multiple columns, I'm trying to handle and generate a human friendly error message but somehow spring/hibernate is stealing the exception from me
#Entity
#Table(name = "columns", uniqueConstraints = {#UniqueConstraint(columnNames = {"position", "board_id"}, name = UNIQUE_COLUMN_BOARD_POSITION)})
public class Column {
//some stuff
}
Then i have the service class
#Transactional
#NonNull
public Column update(long columnId, #NonNull Column transientColumn) {
Column persistentColumn = get(columnId);
persistentColumn.setTitle(transientColumn.getTitle());
persistentColumn.setPosition(transientColumn.getPosition());
try {
return columnRepository.save(persistentColumn);
} catch (Exception e) {
System.out.println(e); //NEVER REACHES HERE
}
return transientColumn;
}
I'm writing my test clases so I autowired the service class, I intentionaly try to update a column to break the unique contraint, hibernate stops the update throw a giant exception... BUT WONT LET ME CAPTURE THE EXCEPTION
I've debugged line by line, the code reaches the save line, then spring/hibernate code starts to run, after that the exception is thrown but my catch clause never gets it and the test finishes
here is the test class
#Test
void testFailUpdateDueRepeatedPosition() {
Column column = new Column(persistentBoard, "column", 1);
Column column2 = new Column(persistentBoard, "column", 2);
column = columnRepository.save(column);
column2 = columnRepository.save(column2);
column2.setPosition(1);
columnService.update(column2.getId(), column2);
columnRepository.delete(column);
}
I don't care if I can't catch this exception all i want is to have a custom message which i can set in case it happens
I have a remove() in my DAO class and sometimes I got error because
FOREING KEY violation
. I.E: When user try to remove a Product that is used by a Customer.
This is my remove() method:
public void delete(AbstractEntity entidade) throws DAOException {
try {
entidade = getEm().merge(entidade);
getEm().remove(entidade);
} catch (Exception e) {
logger.error(e);
throw new DAOException(e.getMessage());
}
}
The problem is that error just appear when database touched. I need to validate some exceptions in this point to send a right Exception to USER, like: "You can delete this because other information is using it."
I have a unique constraint on one of my entities and whenever I get a PSQLException which occurs whenever that constraint is violated, I want to respond with a bad request.
This is my exception handler which I tried to implement:
#ControllerAdvice
public class DatabaseExceptionHandler {
#ExceptionHandler(value = PSQLException.class)
#ResponseStatus(HttpStatus.BAD_REQUEST)
public void handleDatabaseExceptions(PSQLException e) {
// i want to respond with a bad request only when this condition is satisfied
//
// if (e.getSQLState().equals("23505")) {
//
// }
}
}
And this is where the model is saved in db:
public DepartmentForHoliday setDepartment(DepartmentForHoliday department) {
if (department.getDepartmentId() == null) {
Department savedDepartment = new Department();
savedDepartment.setName(department.getName());
try {
departmentRepository.save(savedDepartment);
} catch (PSQLException e) {
/*here i have a compiler error which says that this exception is never thrown in the corresponding try block, but where ?*/
}
}
This is the exception that is thrown when I add a duplicate entry:
org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "uk_1t68827l97cwyxo9r1u6t4p7d"
Detail: Key (name)=(Tech) already exists.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2458) ~[postgresql-9.4.1211.jre7.jar:9.4.1211.jre7]
How to handle PSQLExceptions ? Should I make my own exception as a wrapper or how to solve this problem ?
Key problem is that PSQLException is wrapped into some Spring exception (which I assume from your code); you have to unwrap it (for example using guava's Throwables):
public DepartmentForHoliday setDepartment(DepartmentForHoliday department) {
if (department.getDepartmentId() == null) {
Department savedDepartment = new Department();
savedDepartment.setName(department.getName());
try {
departmentRepository.save(savedDepartment);
} catch (RuntimeException e) {
Throwable rootCause = com.google.common.base.Throwables.getRootCause(e);
if (rootCause instanceof SQLException) {
if ("23505".equals(((SQLException) rootCause).getSQLState())) {
// do smth interesting :)
}
}
}
}
}
Once you do that you can throw your custom exception and handle it in DatabaseExceptionHandler
You are catching PSQLException. Instead of that, please catch SQLException. With SQLException you will can handle all this SQL exceptions.
You can check the SQLException knowledge at this link
Then in your code just treat the SQLException as you want. The most generic catch clause is the following one:
catch (SQLException e)
{
System.out.println("ERROR: Fetch statement failed: " +
e.getMessage());
}
With this code you are printing the exception. If you want more information, check this
This is quite late, but building on previous responses I was able to solve it as so:
try {
return this.projectRepository.saveAndFlush(patchedProjectEntity);
} catch (DataIntegrityViolationException e) {
if (e.getMostSpecificCause().getClass().getName().equals("org.postgresql.util.PSQLException") && ((SQLException) e.getMostSpecificCause()).getSQLState().equals("23505"))
throw new UniqueConstraintViolationException("", e.getMostSpecificCause());
throw e;
}
Where UniqueConstraintViolationException is a custom exception and handled with a spring controller advice.
You might as well register an exception handler for that wrapped exception (that #radek mentioned) directly.
In your case that's:
#ExceptionHandler(DataIntegrityViolationException::class)
protected fun handleDataIntegrityException(ex: DataIntegrityViolationException, request: WebRequest) : ResponseEntity<SomeBody>{
return ResponseEntity.badRequest().body(someBodyHere)
}
The error is converted within convertHibernateAccessException in org.springframework.orm.jpa.vendorHibernateJpaDialect, which has already processed away from PSQL. You can add a breakpoint there and follow the stacktrace.
There is a lot of proxy'ing happening under the hood, but the takeaway is that there is always a readable, expressive Exception to use directly.
I having a spring method: where I am validating the entity after constructing of object, which was previously fetched from DB.
#Transactional(rollbackFor={ValidationException.class})
public Object approve(Command command) throws ValidationException {
Object obj = merger.mergeWithExistingobject(command); // where I am fetching object from DB with old values and construct the new object
validatorService.validate( obj ); // which throws ValidationException
return saveObject(obj);
}
But unfortunately even after the ValidationException was thrown. The values still get persisted in DB. How can I avoid this situtation.
You can evict the entity on ValidationException:
try {
validatorService.validate( obj );
} catch (ValidationException e) {
entityManager.detach(obj);
//Or with Hibernate API
//session.evict(obj);
throw e;
}
I've never used spring but if this is the same as EJB then transactions are only rolled back when unchecked exceptions are thrown.
When a java.lang.Exception is thrown from the EJB method, you obviously have to declare it in the method's signature with thethrows keyword. What happenes in this case as follows 1) the transaction gets commited 2) the exception is rethrown to the client
Taken from this link
I have a managed stateless session bean with injected EntityManager em.
What I am trying to do is to have a database table with unique column. Then I run some algorithm which is trying to insert an entity. If entity exists however it will update it or skip it.
I would like to have something like this:
try {
em.persist(cd);
em.flush();
} catch (PersistenceException e) {
// Check if the exception is DatabaseException and ConstraintViolation
// Update instead or skip it
}
Problem is that I am able to catch only PersistenceException. DatabaseException is not catched. It is sad because only DatabaseException has method called getDatabaseErrorCode() I would like to use to check duplicate entry. I dont understand it because PersistenceException.getCause() returns DatabaseException.
So my question is: How do I catch DatabaseException and check the MySQL error code?
Thank you for any ideas and experiences with this.
I have a suggestion which is I use in my application. We can retrieve the SQLException from PersistenceException. After that, try to get sql error code for SQLException. If your requirement is to get the sql error code, your can follow my example;
public void insert(Group group) throws DAOException {
try {
//your operation
em.flush();
logger.debug("insert() method has been successfully finisehd.");
} catch (PersistenceException pe) {
String sqlErroCode = getErrorCode(pe);
// do your operation based on sql errocode
}
}
protected String getErrorCode(RuntimeException e) {
Throwable throwable = e;
while (throwable != null && !(throwable instanceof SQLException)) {
throwable = throwable.getCause();
}
if (throwable instanceof SQLException) {
Properties properties = --> load sql error code form configuration file.
SQLException sqlex = (SQLException) throwable;
String errorCode = properties.getProperty(sqlex.getErrorCode() + "");
return errorCode;
}
return "NONE";
}
Example error code configuration of mysql
mysql_error_code.properties
#MySQL Database
1062=DUPLICATE_KEY_FOUND
1216=CHILD_RECORD_FOUND
1217=PARENT_RECORD_NOT_FOUND
1048=NULL_VALUE_FOUND
1205=RECORD_HAS_BEEN_LOCKED