• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

JPA Unique collumns

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
f
 
reply
    Bookmark Topic Watch Topic
  • New Topic