• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DataIntegrityViolationException from concurrent access

 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a table that has a unique index constraint on two columns. My bit of code that does the check and insert into the database is as below:



Is there any possibility that the above code could result in a DataIntegrityViolation exception because of concurrent access tp the above method? I have a unique index defined on a column called name and another column called version. Every time I call this createNewEntry method, I check if one exists if yes, I copy everything, increment the version and insert the new version in the database.

Since I'm using Slick for database access, the above method works within a database transaction context and in auto commit mode.

Now the question is could there be a scenario where I could have a DataIntegrityViolation exception? Should I guard against it?
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exact details depend on the database and transaction serialization level you're using, but in general it is possible for that exception to happen in concurrent environment, so you should handle it. If the only code inserting into the table is the one you've shown, the probability of this happening is rather low, I'd say, but still nonzero.
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The system in which that bit of code would run is build using Scala's Future and Async libraries. So I guess it is better if I guard against any non-determinism that might arise due to concurrent access! I will try to find out a solution to that!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic