> From: [EMAIL PROTECTED]
> Date: Tue, 26 Jun 2001 14:54:13 +0200
> 
> As a follow-up to the debate on how to get auto-increment primary
> keys:
> 
> Is it feasible to use a random number generator to generate primary
> keys? I don't really need my records to have steadily increasing keys
> and my number of records will presumably be much smaller than the size
> of my value space (4 billion? depending on data type for prim-key).

If your database provides sequences, use them. There are several problems with
using 'random' numbers:

1. How random are the numbers? It's possible to get a synonym, at which point
   you've got to catch the db error and start all over.

2. A sophisticated database will optimize it's use of a disk, especially a
   SCSI disk, and lay records down in such a way that the heads insert/read
   sequential data very efficiently. 'Random' keys will checkerboard your data.

Keys should meet two criteria. Unique, and sequential. 

What if your db doesn't provide sequences? If you can guarantee that you'll
only run in a single jvm, append a static counter to the end of the time stamp
in milis. Remember that dos machines are on an 8 mili cycle, so you'll need
that static variable as a tie breaker. (This is how weblogic sets
cookies). When the time-stamp changes, reset your counter to zero, so that
when your rock-solid, fault-tolerant app has been up for nine months the
primary keys aren't _huge_. 

If you're going to run in multiple jvm's, the solution is a counter
table. Lock the counter table when you update the count. This doesn't perform
as bad as it sounds.

kb

-- 
Kenneth Bowen [EMAIL PROTECTED] /\ moc.xobop@newobk newoB htenneK
http://www.pobox.com/~kbowen  /  \ newobk~/moc.xobop.www//:ptth
Powered by Debian 2.2 | Mcq! /    \!qcM | 2.2 naibeD yb derewoP


_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to