Guy Rouillier wrote:

> This approach will not work reliably.  Between your insert of nextval and
> your select of currval, someone else may have inserted another row, such
> that your currval will actuall retrieve the wrong currval.  I suppose you
> could address this with synchronized, but a better way (IMO) is to retrieve
> the nextval into your code.  You can then use it for the insert and also no
> the value for any further use you may have, and not have to worry about
> multi-user issues.

I believe that Oracle guarentees isolation of sequence values between 
transactions - the value you get on from 'seq.nextval' will be what you 
(in the same transaction) get from the next 'seq.curval' regardless of 
what anybody does in another transaction.


> 
> We always wrap the sequence with a getter function, and this has saved our
> hides many times.  For example, let's say you start by using the sequence
> number as your primary key.  Then someone comes along and decides that they
> want the IDs to look like "CS00001".   You may have to change code in
> several places if you just access the sequence directly, but if you wrap it
> in a getter function, you don't have to change anything but that getter
> function.
> 

Having said the above, I agree with your approach completely - I always 
get the sequence value first, within a RequiresNew transaction. This is 
also for portability - I'm not reliant on Oracle's semantics, and it's 
easy to port to PostgreSQL's sequence implementation or whatever you 
need to do in other databases.

-danch



_______________________________________________
JBoss-user mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-user

Reply via email to