简体   繁体   English

如何通过Oracle数据库更改通知获取表的主键

[英]How Can I get table's primary key via Oracle Database Change Notification

I could get notifications from an Oracle database thanks to this code and omitting this line: 得益于此代码 ,我可以从Oracle数据库中收到通知,并省略以下行:

prop.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION,"true");

Also I could solve my ORA-29977 problem changing select * from act_code_metadata where product_id=1159 with select column_with_Number_type from act_code_metadata where product_id=1159 我也可以解决我的ORA-29977问题, select column_with_Number_type from act_code_metadata where product_id=1159 select * from act_code_metadata where product_id=1159中的select column_with_Number_type from act_code_metadata where product_id=1159进行select column_with_Number_type from act_code_metadata where product_id=1159

Everything works as expected :D 一切都按预期进行:D

This is the code I use to print the row's information (Java 8): 这是我用来打印行信息的代码(Java 8):

DatabaseChangeRegistration dcp.addListener((DatabaseChangeEvent dce) -> 
                System.out.println(
                "Changed row id : " +
                dce.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowid().stringValue() 
                + " " +  dce.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowOperation().toString()));

But all the information I get is the row's physical address (rowid) and the operation involved (insert, delete or update). 但是我得到的所有信息都是该行的物理地址(行号)和所涉及的操作(插入,删除或更新)。 I need to identify the row being modified/inserted/deleted to refresh my cached data in several Swing controls in my GUI. 我需要确定要修改/插入/删除的行,以刷新GUI中多个Swing控件中的缓存数据。

I've read that , despite the rowid being imutable , the same rowid can be re-assigned if the row is deleted and a new one is inserted, and the rowid can change if the row is in a partitioned table. 我已经读到尽管rowid不可更改 ,但如果删除行并插入新的rowid,则可以重新分配相同的rowid;如果行在分区表中,则rowid可以更改。 So the best that can be done is using the rowid and the row's primary key. 因此,最好的办法是使用rowid和行的主键。 My table has a autoincrementable primary key (with a sequence and a trigger) created with this code . 我的表具有以此代码创建的可自动递增的主键(带有序列和触发器)。

I have no control on what happens on the database or if somebody inserts and deletes rows several times. 我无法控制数据库上发生的事情,或者有人多次插入和删除行。 So I can get the wrong row when selecting it using the rowid given by the notification. 因此,使用通知提供的rowid选择行时,我可能会得到错误的行。

Is there a way that I can get my row's primary key via Oracle Database Change Notification so I can identify the inserted/deleted/modified row correctly? 有没有一种方法可以通过Oracle数据库更改通知获取行的主键,以便正确识别插入/删除/修改的行?

I'm working with Oracle Database XE 11.2 Express and Java 8. The user for database connection already has the change notification privilege. 我正在使用Oracle Database XE 11.2 Express和Java8。用于数据库连接的用户已经具有change notification特权。

It seems that you have a lot of overhead trying to basically maintain a fresh snapshot of the data in your GUI. 试图在GUI中基本上维护数据的新快照似乎有很多开销。 It may be simpler to look at client result caching and just re-running your query every X seconds; 查看客户端结果缓存并每隔X秒重新运行一次查询可能会更简单; and let Oracle do the magic of seeing if the data changed. 让Oracle神奇地查看数据是否已更改。 You would be limited to a JDBC driver that supports OCI. 您将仅限于支持OCI的JDBC驱动程序。 See http://docs.oracle.com/cd/E11882_01/server.112/e41573/memory.htm#PFGRF985 for details. 有关详细信息,请参见http://docs.oracle.com/cd/E11882_01/server.112/e41573/memory.htm#PFGRF985 With client result caching, the first time the SQL is executed, it will take say 500 milliseconds. 使用客户端结果缓存时,第一次执行SQL时将花费500毫秒。 Next query using the same criteria it will take 2 or 3 milliseconds. 使用相同条件的下一个查询将花费2或3毫秒。 Assuming the result set is small (less than 100 rows is quite small), you can get a lot better results without all that framework you are trying to build. 假设结果集很小(少于100行就非常小),则无需尝试构建所有框架就可以获得更好的结果。

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

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