• 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
Sheriff
Posts: 15767
74
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
 
Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!