Java - oracle.jdbc.dcn.DatabaseChangeEvent - 获取更改的行

时间:2013-12-28 22:43:49

标签: java sql oracle events jdbc

我正在使用oracle.jdbc.dcn.DatabaseChangeEvent来从我的Oracle DB获取事件通知:

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

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

在另一个地方:

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 (); 
}

我的问题:有没有办法知道插入\更新\更改\ dalteted的确切行?

我的目的是监控数据库并在发生某些特定事件时执行某些操作,因此我希望能够知道新的\更新行值以通知监控系统(和避免每次事件都进行全表扫描。)

重要提示:除了“选择”功能外,我无权访问数据库,因此无法在此处触发。

谢谢。

1 个答案:

答案 0 :(得分:1)

如果您通过设置OracleConnection.DCN_NOTIFY_ROWIDS来致电registerDatabaseChangeNotification,则可以使用ROWID s:

您的DatabaseChangeEvent公开TableChangeDescription[] getTableChangeDescription()功能。如果包含非null每个更改的表格中的一个对象,并且可以调用RowChangeDescription[] getRowChangeDescription(),那么您最终可以通过调用ROWID来获取ROWID getRowid()

您可能还需要在数组的每个部分调用getQueryChangeDescription()后评估TableChangeDescription的结果getTableChangeDescription() - 对象。

可以根据ROWID使用

documentation来选择或更新存储在Oracle DB中的行。