• 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

key-generator

 
Ranch Hand
Posts: 291
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have heard a bit about primary key generators lately on the list.I am interested in writing a simple one. What are the goals a good generator should accomplish? Any pitfalls to watch out for? Maybe someone could point out some good resources on the web?
Thanks,
ELahe
 
Saloon Keeper
Posts: 27762
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A primary key generator is "good" when it generates unique keys. Otherwise it's defective. The most common cause of failure is in multi-threaded systems where the process isn't atomic and the same key can be generated twice because the internal value wasn't updated before another request came in.
The easiest generation technique is just to use an integer and increment it for each new row added to the table. Many DBMS's have the ability to do this for you automatically.
When working with CMP EJB's, there's a problem with this, though, since CMP EJBs expect ALL values for a newly-created EJB to be supplied by the constructor and not acquired from the DBMS. TO get around this, I use a special EJB that has a "createNewKey()" method. Because it's an EJB, the atomicity of the operation is ensured by the EJB transaction mechanism.
reply
    Bookmark Topic Watch Topic
  • New Topic