简体   繁体   中英

Get RowId from QueryChangeDescription

To my Oracle Database I have registered a DatabaseChangeNotification with a SQL-Query. I like to get any changes on the defined TABLE .

DatabaseChangeRegistration changeRegistration;

Properties properties = new Properties();
properties.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
properties.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION, "true")

changeRegistration = connection.registerDatabaseChangeNotification(properties);

DCNListener dcnListener = new DCNListener(this);
changeRegistration.addListener(this);

Statement statement = connection.createStatement();
((OracleStatement) statement).setDatabaseChangeRegistration(changeRegistration);
String sql = "SELECT * from TABLE";

statement.executeQuery(sql);

In my Listener I receive the DatabaseChangeEvents

public void onDatabaseChangeNotification(DatabaseChangeEvent databaseChangeEvent) {
  TableChangeDescription[] tableChangeDescription = databaseChangeEvent.getTableChangeDescription();
  QueryChangeDescription[] queryChangeDescription = databaseChangeEvent.getQueryChangeDescription();
  for (QueryChangeDescription qcd: queryChangeDescription) {
    String result = qcd.toString();
    System.out.println(qcd);
  }
}
  1. tabelChangeDescription is null
  2. My result is:

query ID=201, query change event type=QUERYCHANGE Table Change Description (length=1): operation=[UPDATE], tableName=USER.TABLE, objectNumber=67385 Row Change Description (length=1): ROW: operation=UPDATE, ROWID=AAAQc5AAHAAAAG/AAB

Is there any nice way to get the ROWID from the Changes row else than String-Parsing? I can't find any getRowId -Method on the QueryChangeDescription .

I found the was to get the RowId. From the queryChangeDescription you can get the TabeleChangeDesciptions which has nothing in Common with the TableChangeDecription from the event. If there are changes on more than one Table, these tables where listed in the Array. Because I am registered to only one Table I don't have to iterate over the list.

After habing the the TableChangeDescription you can get the RowChangeDescription for each changed row. From this you can get the RowId.

for (QueryChangeDescription queryChangeDescription : databaseChangeEvent.getQueryChangeDescription()) {
  RowChangeDescription[] rowChangeDescriptions = queryChangeDescription.getTableChangeDescription()[0].getRowChangeDescription();
  for (RowChangeDescription rowChangeDescription : rowChangeDescriptions) {
    handleEvent(rowChangeDescription.getRowid());
  }
}

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