简体   繁体   English

字典数据插入或选择(oracle,java)

[英]dictionary data insert or select (oracle, java)

I have an table (in ORADB) containing two columns: VARCHAR unique key and NUMBER unique key generated from an sequence. 我有一个包含两列的表(在ORADB中):VARCHAR唯一键和从序列生成的NUMBER唯一键。

I need my Java code to constantly (and in parallel) add records to this column whenever a new VARCHAR key it gets, returning the newly generated NUMBER key. 每当需要新的VARCHAR键时,我都需要Java代码不断(并并行)向该列添加记录,并返回新生成的NUMBER键。 Or returns the existing NUMBER key when it gets an existing VARCHAR (it doesn't insert it then, that would throw an exception of course due to the uniq key violation). 或在获取现有VARCHAR时返回现有的NUMBER键(然后不插入该键,由于uniq键违规,这当然会引发异常)。

Such procedure would be executed from many (Java) clients working in parallel. 这样的过程将从许多并行工作的(Java)客户端执行。

Hope my English is understandable :) 希望我的英语是可以理解的:)

What is the best (maybe using PL/SQL block instead of Java code...) way to do it? 什么是最好的方式(也许使用PL / SQL块而不是Java代码...)?

I do not think you can do better than 我不认为你可以做得更好

  1. SELECT the_number FROM the_table where the_key = :key

  2. if found, return it 如果找到,将其退回

  3. if not found, INSERT INTO the_table SELECT :key, the_seq.NEXT_VAL RETURNING the_number INTO :number and COMMIT 如果未找到,则INSERT INTO the_table SELECT :key, the_seq.NEXT_VAL RETURNING the_number INTO :numberCOMMIT

  4. this could raise a ORA-00001(duplicate primary key insert) if the timing is unlucky. 如果时机不佳,则可能会产生ORA-00001(重复的主键插入)。 In this case, SELECT again. 在这种情况下,请再次SELECT

Not sure if JDBC supports RETURNING, so you might need to wrap it into a stored procedure (also saves database roundtrips). 不知道JDBC是否支持RETURNING,因此您可能需要将其包装到存储过程中(还保存数据库往返)。

You can use an index-organized table (with the_key as primary key), makes the lookup faster. 您可以使用索引组织的表(以the_key作为主键),从而使查找速度更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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