A clean and fast method that I tend to use in non-EJB applications  is to
have a singleton class that grabs a new ID from the database just once
when it's initialized. I call this the session part of the key. I then use
a counter variable that's incremented by one each time some object asks
for a new ID. It is combined with the session ID into a larger number and
returned. I also handle wrap-arounds and fetch a new id from the database
when needed. Since I don't use database-specific features in order to make
it more generic, I tend to use three int's for the session part and one
int as the counter part. I zero-pad them and concatenate them into a 40
character string, which fits nicely in a CHAR(40) column in the database.
It's true that using a NUMBER column probably would be somewhat faster,
but I haven't experienced any performance problems because of this.
Another approach would be to use two int's and combine them into a long,
which would fit into a NUMBER column.

The advantage with this approach is that it runs almost entirely in
memory, yet creates unique ID's

/Robert





Alasdair Gilmour <[EMAIL PROTECTED]>
Sent by: A mailing list for Enterprise JavaBeans development
<[EMAIL PROTECTED]>
2000-05-16 10:36
Please respond to A mailing list for Enterprise JavaBeans development


        To:     [EMAIL PROTECTED]
        cc:
        Subject:        Re: Creating Entity Beans with db generated sequence numbers

One possibility is to grab a 'chunk' of ID's at one time. e.g. client
asks for an ID, database counter is incremented by 100, and the client
has effectively reserved ID's 1-100 for its use. Until it uses up this
batch of IDs, the client need not hit the database again to request a
new ID. When it does, it gets given another batch etc. etc. This can
vastly reduce the number of expensive database operations required. The
size of these chunks obviously depends on your
application/performance/memory constraints and so on.

Another approach I've seen used successfully is to just pick a random
integer as an ID, and attempt to use it. If there already happens to be
a row with the same primary key, the database throws an Exception, and
you simply generate a new random number, and try again etc. etc. In
practice, unless you have hundreds and hundreds of millions of rows,
primary key clashes are very unlikely, so performance is not a problem.

Cheers

Alasdair


> Hi All,
>
> I have a question regarding Creating CMP Entity EJBs.
> For several of our Entities, we use a sequence number generated by our
> database (Oracle) as a primary key.
> The problem is that our ejbCreate() method expects a primary key when
> invoked. Also, the persister generated create methods appear to be
called
> before even the first line of the bean's ejbCreate().
>
> To get around this, at the controller level, we were able to create a
> separate Connection, query the database for the next sequence number,
close
> the connection, and then call the EJBHome Create() method passing in the
> sequence number.
> Although it works, this approach seems very clumsy and slow.  There's
got to
> be a better way,
> Perhaps where we don't even need a separate database connection.
>
> I assume many of you are also using database generated sequence numbers.
> Have any of you run into this same problem? Any ideas for a more elegant
> solution?
>
> Thanks,
> Dan
>
>
===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff EJB-INTEREST".  For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".
>
>
===========================================================================
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff EJB-INTEREST".  For general help, send email to
> [EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to