2

Each time I save() a new domain object in Grails I see Hibernate issuing Select statements for each of the unique constraints in my Grails domain class.

If I save without validation, I don't see the Select statements. Example:

new Person(name: 'Foo').save(validate: false)

Is there a way to stop these extra Select statements from occurring while still having validation enabled?

One of my goals is to decrease appserver/database chattiness since I am expecting my Grails app to run on a high latency network (e.g., communicating from Grails app => DB can be costly).

Burt has one suggestion, but I'm hoping for something easier/cleaner:

This may be a Hibernate issue more than Grails. I'm not sure.

Maybe some other related info:

Community
  • 1
  • 1
Vahid Pazirandeh
  • 1,552
  • 3
  • 13
  • 29
  • 1
    Hopefully, you now see why it does those selects. Validation is determining if your uniquely constrained field is actually unique, and doesn't use a failure to insert to do so, as that wouldn't allow Grails to respond with WHICH field failed. – billjamesdev Sep 20 '14 at 05:40
  • @BillJames right, I began realizing that after I posted. However I was hoping to parse the SQL error message myself (DB constraint name => Grails field maybe, not sure if possible) and re-throw the exception as a Grails ValidationException. Let me know if you can think of a way other than Burt's to stop these Selects. I don't want to resort to monkey patching, but maybe as a last resort. :) – Vahid Pazirandeh Sep 20 '14 at 19:57
  • 1
    Well, no, I don't know a way to change the functionality of the unique constraint... You have to stop using it if you don't want it to do it's job. – billjamesdev Sep 21 '14 at 15:33
  • A Grails constraint is supposed to be able to validate the object before inserting or updating it on the DB. For a Unique constraint, the only way to do so is to issue a query to check whether there exists already a conflicting record. You can see the source code for the unique constraint [here](http://is.gd/egyM1g). If that's not what you want, you might want to define your own constraint that does away with the select and/or add client code to deal with the unique key exception arising from the DB. You might want to investigate the options Hibernate provide in this regard. – Tobia Sep 30 '14 at 22:09

0 Answers0