简体   繁体   中英

Oracle 11g JDBC prefetching keys

My app needs to know which row receives which key in the database.

I am using JDBC on read commited isolation level with batch updates. Therefore retrieving the keys with getGeneratedKeys isn't possible. My plan is to fetch a Range of ids from the database in a transaction safe way, eg two parallel transactions can't get the same keys.

Can I write some SQL-Query with the number of future rows as an argument to fetch a list of future keys in Oracle 11g?

What are my options here?

you can use sequences for this purpose:

create sequence s;

to retrieve a id

select s.nextval from dual;

or to retrieve a range of ids :

select s.nextval from dual connect by level <= 10;

Just wanted to add:

There is no explicit relationship between sequences and tables. A sequence is simply a mechanism for generating numerical sequences. You can assume that a sequence will never produce a duplicate value, and that's about it. Don't assume order, and definitely don't expect a gap-free sequence. Sequences are done this way in Oracle, by design. By following those rules, and making no other assumptions about a sequence's behavior, we get extremely scalable performance with sequences. Note that as soon as you try to enforce an order or a gap-free sequence, your performance suffers severely.

In summary: 1.) Sequences are dead simple, and really fast. 2.) Other than "no dups", don't assume you can predict a sequence's behavior. 3.) Sequences are independent objects, not tied to a table, and can be used to generate sequence values that will be used in the same table, or many different tables.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM