This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unique constraints  RSS feed

 
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: 16026
87
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
Boost this thread!