GeeCON Prague 2014*
The moose likes JDBC and the fly likes how to synchornize Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Databases » JDBC
Bookmark "how to synchornize "select"?" Watch "how to synchornize "select"?" New topic
Author

how to synchornize "select"?

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1871
hi all
i'm running a query,
select max(idfield) from mytable query to get the maximum ID generated so far and then generating a new ID as the max(idfield)+1.
i use oracle database (i donno if there is a autoincrement in there). now, my problme is, i have the above query in a servlet so if two client request comes simultaenously and both of them get the same max(idfield) as a select query then i'd end up generating same ID for both of the request, right?
how can i synchroniz this access? will,
synchronize {
- run select query
- generate new id
}
will work?
regards
maulin
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Oracle has sequences which you should use for creating the IDs.
Here's how you create a sequence in the database (see Oracle documentation for more options):

Once you've got a sequence set up, here's how you fetch an "auto-incremented" value from the sequence:


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
GeeCON Prague 2014
 
subject: how to synchornize "select"?