• Post Reply Bookmark Topic Watch Topic
  • New Topic

JPA Unique collumns  RSS feed

Miltos Tereres
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello i have a unique collumn... @Column(unique=true)...

The problem is that i want to inform the user if he attemps to persist something that violates the uniqueness.
Catching the error is a bit difficult because it is vendor specific and i should do a

em.flush() otherwise the transaction will commit after the end of the method

I see that this solution isnt good and although that it catches the exception multiple exceptions are thrown before and after this catch and polute the server log.Isnt this a perfonace problem?
The alternative would be a query but what if this scenario happens?
eg user1 want to persist an exam with codename MPLAH ,user2 want to persist an exam with the same codename MPLAH...
checkifcodenamexists (user1) and then checkifcodenameexists(user2)... both would return false...
then persist(user1) would be ok persist(user2) would cause an error!
I know that this would be an nearly impossible case (2 users at the very same time try to insert something with the same value) but generally i would like to know what i can do about...

Someone told me that locking would be a solution..However if i lock the table the exception should be thrown from createExam and no before calling this method at validation time...
any ideas?
thank toy
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!