File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

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="http://www.mycgiserver.com/~nan111/index.html" target="_blank" rel="nofollow">www.mycgiserver.com/~nan111/index.html</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.
Cheers..
Daman
Jim Baiter
Ranch Hand

Joined: Jan 05, 2001
Posts: 532
Check out http://sourceforge.net/projects/ejbutils
These are some freebie utilities one of which is a uid generator.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Better way to generate Primary Keys in EJB ?
 
Similar Threads
Please help with JDBC/DataSource transactions and primary key!
Id field is a PK but is also a FK pointing towards a PK of a main table
IBM 287 ICE TEST
Entity Bean -- Few Questions
To Nikhil Pendharkar for IBM 287