Handling Duplicate Grails Duplicate

How to catch duplicate key exceptions in Grails. when trying to store an existing integer for a unique column constraint, an error is generated when saving / updating the record.

Also used

try{
    object.save(flush:true)
}
catch(org.springframework.dao.DataIntegrityViolationException e){
    println e.message
}
catch(org.hibernate.exception.ConstraintViolationException ex){
    println e.message
}
catch(Exception e){
    println e.message
}

      

But failed to catch this issue.

23: 41: 13.265 ERROR [JDBCExceptionReporter: 101] Duplicate record '1' for key 2
23: 41: 13.281 ERROR [AbstractFlushingEventListener: 324] Failed to sync database
with session org.hibernate.exception.ConstraintViolationException: Failed to do JDBC batch update in org.hibernate.exception.SQLStateConverter.convert (SQLStateConverter.java:94) in org.hibernate.exception.JDBCExceptionHelper.convert (JDBCExavaceptionH66) at org.hibernate.jdbc.AbstractBatcher.executeBatch (AbstractBatcher.java:275) at org.hibernate.engine.ActionQueue.executeActions (ActionQueue.java:266) at org.hibernate.engine.ActionQueue.executeActions (Action168ueue.java ) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions (AbstractFlushingEventListener.java:321) at org.hibernate.event.def.DefaultFlushEventListener.onFlush (DefaultFlushEventListener.javah:50) at org. SessionImpl.java:1027)

Could you please suggest a solution to solve this problem.

+2


a source to share


5 answers


You are trying to save a record with a pre-existing ID.



  • If the ID is auto-generated, do not set it manually
  • If the id is not automatically generated then set it to something else like max(id) + 1

0


a source


Of course an exception shouldn't be thrown to violate the constraints, but should object.save () return null? i.e.



if(object.save() == null) {
    // save failed
} else {
    // save succeeded
}

      

0


a source


If you've determined uniqueness through a Grails constraint, then you need to look for a ValidationException. This is thrown when object.validate () fails; The check is done before any .save () object.

try {
  object.save(failOnError: true)
}
catch(ValidationException ve) {
  // Do something ...
}

      

But remember: any constraint violation on any member variable can throw a ValidationException ... so you need to differentiate yourself.

Edit:

This is applicable if you are using Grail 1.2 failOnError ...

0


a source


I'm looking for the same problem, so maybe not a complete answer, but what you can do is force check and see errors, identify the case, and put the actions you want:

if(instance.validate()) {
    //everything ok 
} else {
    instance.errors.each {
            //identify the case and place actions
    }
}

      

Also note that the error is: className.propertyName.unique

0


a source


Maybe it should work:

import org.springframework.dao.DuplicateKeyException

try {
    domainInstance.save(flush: true)
} catch(DuplicateKeyException e) {
    // ...
}

      

0


a source







All Articles