开发者

Grails Duplicate Exception handling

How to catch duplicate key exceptions in Grails . when trying to save existing integer for a unique column constraint, the error is generating while saving/updating a 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 unable to catch this issue .

23:41:13,265 ERROR [JD开发者_如何学运维BCExceptionReporter:101] Duplicate entry '1' for key 2

23:41:13,281 ERROR [AbstractFlushingEventListener:324] Could not synchronize database

state with session org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:94) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66) at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:275) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:266) at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:168) at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321) at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50) at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)

Could you please share the solution to solve this .


You're trying to save a record with an id that already exists.

  • If id is auto-generated, don't set it manually
  • If id is not auto-generated, set it to something else, for example max(id) + 1


surely no Exception should be thrown for constraint violation, but rather object.save() should return null? i.e.

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


If you defined the uniqueness through a Grails constraint, you have to look for a ValidationException. This is thrown when object.validate() fails; Validation is done before any object.save().

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

But remember: Any constraint violation, for any member variable can cause a ValidationException ... so you have to distinguish yourself.

Edit:

This applies, if you use the Grails 1.2 failOnError feature ...


I am looking for the same problem so maybe not a complete answer but what you can do is to force validation and look in the errors, identify the case and place the actions you want:

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

Also note that error is: className.propertyName.unique


Possibly it should work:

import org.springframework.dao.DuplicateKeyException

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜