Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Unique constraints

 
Michal Kowalski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I've question about strategy with unique constraint in table. What is better? "manually" checking (by additional queries) if the specific value exists in database and then save object or first save object and then catch specific exception if this value exists?
I use hibernate and springframework.
Thanks
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15634
46
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First checking before inserting will not work if there are multiple threads or processes writing to the database: it could happen that you check if a value exists, then another thread or process inserts that value, then the first thread inserts it and gets an exception because the value already exists.
 
Michal Kowalski
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, first persist and then if this fails catch exceptions and fetching more info what goes wrong.
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic