File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes EJB and other Java EE Technologies and the fly likes Better way to generate Primary Keys in EJB ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Better way to generate Primary Keys in EJB ?" Watch "Better way to generate Primary Keys in EJB ?" New topic

Better way to generate Primary Keys in EJB ?

Sri Rangan
Ranch Hand

Joined: Dec 08, 2001
Posts: 160
Our project is large scale one based on J2EE, we use a ID Bean of type Stateless Session bean to generate the next ID (primary Key).Next ID is generated by reading the next ID from table and immediately updating that table with the new ID.Meaning One select and update statement in nextID() method.(Table has only one row with 2 columns - Sl_No and next_ID.)
Settings on this ID bean is TX_REQUIRES_NEW so that the transaction is commmited immediately and leavig way for the next client.

When ever a transaction needs to add a new row to the database, it will call the ID Bean by passing the table name and will get the next ID.
Please note that we have only one ID bean for the entire application. It is definitely will affect the response time because this ID bean is used by all the users. In a particular transaction, one user will insert 180 records to the table.
We do not go with any Database or EJB specific Primary Key Geneartion method.
I would like to know whether this ID generation could be improved in multi user environment or any other better / proven implemenation is availabe in the J2EE world?
nan sh
Ranch Hand

Joined: Jan 05, 2001
Posts: 167
I think the problem is you want using stateless Bean to Update your Next ID! That it is not correct,
I would use two Beans at your situation, one stateless Bean connect to web and the other one will be Entity Bean Update Database. That can solve your mult-user problem.

Have you tried this Mock Exam Testing Engine yet?<br /><a href="" target="_blank" rel="nofollow"></a>
daman sidhu
Ranch Hand

Joined: Aug 19, 2000
Posts: 184
The best way to update the table would be to write a trigger in the database instead of using the bean to update it. This is a more lighter and more durable technique and would work wherever u call the table from, be it bean or jdbc.
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 532
Check out
These are some freebie utilities one of which is a uid generator.
I agree. Here's the link:
subject: Better way to generate Primary Keys in EJB ?
jQuery in Action, 3rd edition