• Post Reply Bookmark Topic Watch Topic
  • New Topic

Best Approach for Primary key Generation  RSS feed

 
Steven Lau
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have some choices like 1. Depend on database specific key generation support, accessible through stored procedure
2. Build custom key generation engine deployed to database, accessible through stored procedure
3. Use a local primary key generation algorithm to eliminate synchronization delays
4. Use local management and delivery of centrally generated primary keys
Can anyone tell me which is the best choice?
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I often rely on a combination of the two. I build a class (which is a singleton) that calls a stored procedure to retrieve a block of primary keys (say a block of 100) and then stores the current key value in a static variable, which it increments every time it hands out a new key. When the incremented value reaches the end of the key range, it calls the stored procedure to get a new block.
You do have to synchronize the incrementing of the variable, but it still ends up being pretty fast in most cases.
Kyle
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roland, please note that your name violates the JavaRanch naming standard. You must use your real first name - space - your real last name as your display name. Thanks!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!