Meaningless Drivel is fun!
The moose likes JDBC and Relational Databases 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 and Relational Databases
Bookmark "how to synchornize "select"?" Watch "how to synchornize "select"?" New topic

how to synchornize "select"?

Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
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?
Lasse Koskela

Joined: Jan 23, 2002
Posts: 11962
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]
I agree. Here's the link:
subject: how to synchornize "select"?
It's not a secret anymore!