Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

What is the best practice for generating primary keys?

843830Aug 31 2006 — edited Sep 5 2006
What is the preferred design for generating primary keys in a relational database?

We use POJO dataObjects and code directly to JDBC. All of our tables have an integer primary key column named �id�. We usually support 5 to 20 users, on Swing clients, accessing stateless session beans, under WebLogic, through WebServices. Most users need 1 second response time, taking orders, etc., but some users run large processes which necessarily hold transactions open for many seconds.

Options we are aware of:
1) Persist the next id for each table in a row of database table. Increment the id and update this table for each new INSERT.
Problem: Long lasting INSERTing transactions block all other users trying to get the next id.

2) Create the tables with IDENTITY columns, and let SQL Server generate the ids.
Problem: We have some processes which require us to create and work with relational dataObjects in memory, before being persisted. So the only way to establish relationships is to assign temporary ids. Then when these objects are persisted, each temporary id must be substituted with the generated id, in all relational objects.

3) Increment ids in memory, in a Singleton class instance. Do a SELECT MAX(id) from each table to initialize the value for each table, as needed.
Problem: Although we are satisfied that no other clients will ever directly update the database except our application server, there is the possibility that we may someday need to cluster our application server across more than one server. In that case, we will have to establish an independent webservice to provide keys for all instances of the application server.

We appreciate any experienced advice, other options, solutions, known problems, or links to other articles which may help. Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 3 2006
Added on Aug 31 2006
8 comments
1,063 views