RE: Generating primary key locally without extra tables

From: Gentry, Michael \(Contractor\) ("Gentry,)
Date: Wed Jun 21 2006 - 09:09:39 EDT

  • Next message: Gentry, Michael \(Contractor\): "RE: MySql auto generated PK"

    I actually looked into making a PK generator that required no DB access, but when I looked into it, Java didn't support everything I wanted without resorting to JNI and C code. Of course, I was more ambitious in what I was thinking. I wanted it to be similar to what I could do in EOF (hundreds of keys/sec).

    Does anyone here know if Java 1.5 supports obtaining the process ID natively? I think that was my stumbling point before. I was trying to make a key that contained server IP, process ID, timestamp, and counter. Maybe it is time to consider this again. If I could produce a unique string all the time like that, it would make a nice PK generator and your PKs would have some meaning (that is me with my production support hat on).

    As to your questions ...

    A) My gut feeling tells me that wouldn't be a good idea.
    B) You could do that, but it seems pretty problematic to me.

    Maybe some others will have a few good ideas ...

    /dev/mrg

    -----Original Message-----
    From: oyvindharbo..mail.com [mailto:oyvindharboe@gmail.com] On Behalf Of yvind Harboe
    Sent: Wednesday, June 21, 2006 2:18 AM
    To: cayenne-use..ncubator.apache.org
    Subject: Generating primary key locally without extra tables

    Problem:

    I'm using Cayenne for projects where I can't modify the database
    schema on the server.

    These databases use server generated primary keys. MSAccess, MS SQL
    Server and Oracle.

    The number of new records created are very small(max 1/minute), so I'm
    rather insensitive to performance of record creation.

    Q: Does a strategy for generating keys locally exist?

    This strategy should work across databases where Cayenne does not
    support server generated primary keys.

    Getting a primary key by e.g.:

    SELECT MAX(ID)+1 FROM FOO

    This would a) be slow b) risk a collision when inserting.

    Qs:

    a) can I always override a server generated key?
    b) would it be sufficient to use exception handling and retry e.g. N times?

    -- 
    yvind Harboe
    http://www.zylin.com
    



    This archive was generated by hypermail 2.0.0 : Wed Jun 21 2006 - 09:10:08 EDT