These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Using MAX(id)+1 for new row's primary key
PostPosted: Wed Aug 10, 2005 6:25 pm 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
Hello. Since "increment" is a in-memory generator (hence, not recommended for cluster environments), I'm trying to figure out if hibernate can do a SELECT MAX(id)+1 type generation of the id fields?

Our primary keys aren't setup with SEQUENCE, so I have to do this myself in the app code.

Other than do it manually in the Java code, is there anyway I can have Hibernate do a MAX(id)+1 for me?

I noticed there is a "select" generator, however, that seems to get the data from another column.


Top
 Profile  
 
 Post subject: Not safe if ...
PostPosted: Wed Aug 10, 2005 8:11 pm 
Newbie

Joined: Wed Aug 10, 2005 2:36 pm
Posts: 3
... 2 transactions each tries to save a new record at the same time.

they both READ the same MAX(id), and hence will write the same MAX(id)+1 as id for the new objects.

Even if Hibernate can do it, it is just a little bit dodgy?


Top
 Profile  
 
 Post subject: Generator
PostPosted: Thu Aug 11, 2005 12:58 pm 
Newbie

Joined: Mon Mar 21, 2005 12:52 pm
Posts: 11
Location: Chicago, Il
The problem would be that in your clustered environment, would have to lock your entity table before writing the new row and keep it locked until the new row is inserted. You would have to block both reads and writes, since you are depending on the table's contents to make a new id.

Perhaps the solution would be as simple as making a service that listens on a tcp socket, increments an internal counter by one per tcp connection, and outputs the result to the connecting machine. It would seem that the blocking time doing so would be less then locking the database table for r/w in a cluster while a row is being inserted.

_________________
where's my coffee mug?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 15, 2005 10:23 am 
Beginner
Beginner

Joined: Fri Jul 29, 2005 2:11 pm
Posts: 21
Thanks, guys.

I'm not sure if our production environment will permit a listener type service to generate unique ids.

How about a small/separate database table (say, with two columns, attribute char(10) and nextid int) that can be locked to get a unique id.

I'm also pushing the DBA to allow use of a SEQUENCE column in the database.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.