Java - oracle.jdbc.dcn.DatabaseChangeEvent - get c

2019-05-27 14:06发布

问题:

I'm using oracle.jdbc.dcn.DatabaseChangeEvent in order to get events notifications from my Oracle DB:

public class TListener implements DatabaseChangeListener
    {
    ...
    public void onDatabaseChangeNotification(DatabaseChangeEvent e)
    {
        ....

        synchronized( changeNotification ){
            changeNotification.notify();
        }
    }
}

And in Another place:

private void run(PropertiesConfiguration configuration) throws SQLException
{
OracleConnection conn = connect(configuration);     
Properties prop = new Properties();

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

DatabaseChangeRegistration dcr = conn.registerDatabaseChangeNotification(prop);

try
{
    // add the listenerr:
    TListener list = new TListener(this);
    dcr.addListener(list);

    // second step: add objects in the registration:
    Statement stmt = conn.createStatement();


    // associate the statement with the registration:
    ((OracleStatement)stmt).setDatabaseChangeRegistration(dcr);

    ResultSet rs = stmt.executeQuery("select * from GTW_TX");
    while (rs.next()){}

    rs.close();
    stmt. Close (); 
}

My question: Is there a way to know which exactly rows where inserted\updated\changed\dalteted?

My purpose is to monitor the DB and do some actions when some specific events occurs, so I want to be able to know the new\updated row values in order to notify the Monitoring system (And to avoid full table scan with every event).

Important note: I do not have access to the DB beside the ability to "select", so triggers are not possible here.

Thank you.

回答1:

If you call registerDatabaseChangeNotification with OracleConnection.DCN_NOTIFY_ROWIDS set, you can use ROWIDs:

Your DatabaseChangeEvent exposes a TableChangeDescription[] getTableChangeDescription() function. If non-null one object per changed table is included and RowChangeDescription[] getRowChangeDescription() can be called, where you can finally get the ROWID by calling ROWID getRowid()

You may also need to evaluate the result of getQueryChangeDescription() where you also get TableChangeDescription-objects after calling getTableChangeDescription() on each alement of the array.

ROWID can be used according to documentation to select or update rows stored in an Oracle DB.