简体   繁体   中英

Canot get table name with Oracle Database Change Notification

I have to monitor multiple tables for changes. My database is Oracle11g, and I'm using ojdbc6.jar jdbc driver library. The problem is I'm getting "???" instead of table name when database change event occurs. All other data in DatabaseChangeEvent is present, only table name is missing.

I'm using this code bellow I found on Internet which I slightly modified to my needs.

This is output from program:

Connection information  : local=ORATEST01/10.10.60.17:43000, remote=ORATEST01/10
.10.60.17:52683
Registration ID         : 63
Notification version    : 1
Event type              : OBJCHANGE
Database name           : fmvroratest
Table Change Description (length=1)
    operation=[UPDATE], tableName=???, objectNumber=73786
    Row Change Description (length=2):
      ROW:  operation=UPDATE, ROWID=AAASA6AAHAAACEEAAA
      ROW:  operation=UPDATE, ROWID=AAASA6AAHAAACEEAAA

Table changed: ???




package hr.mspoljaric.dcn;

import java.sql.*;
import java.util.*;
import oracle.jdbc.*;
import oracle.jdbc.dcn.*;

public class OracleDCN {
    String URL = "jdbc:oracle:thin:**********************************";
    Properties prop;

    public static void main(String[] argv) {

        OracleDCN dcn = new OracleDCN();
        try {
            dcn.prop = new Properties();
            dcn.run();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    void run() throws SQLException {
        OracleConnection conn = (OracleConnection) DriverManager.getConnection(
                URL, prop);

        Properties prop = new Properties();
        prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
        prop.setProperty(OracleConnection.NTF_LOCAL_TCP_PORT, "43000");

        DatabaseChangeRegistration dcr = conn
                .registerDatabaseChangeNotification(prop);

        try {
            dcnListener list = new dcnListener(this);
            dcr.addListener(list);

            Statement stmt = conn.createStatement();
            ((OracleStatement) stmt).setDatabaseChangeRegistration(dcr);
            ResultSet rs = stmt
                    .executeQuery("select 1 from klijent, osobe where 1=2");
            rs.close();
            stmt.close();

        } catch (Exception e) {
            // clean up our registration
            if (conn != null)
                conn.unregisterDatabaseChangeNotification(dcr);
            e.printStackTrace();
        } finally {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        try {

            Thread.currentThread().join();

        } catch (Exception e) {
            e.printStackTrace();
        }

        finally {
            OracleConnection conn3 = (OracleConnection) DriverManager
                    .getConnection(URL, prop);
            conn3.unregisterDatabaseChangeNotification(dcr);
            conn3.close();
        }
    }
}

class dcnListener implements DatabaseChangeListener {
    OracleDCN dcn;

    dcnListener(OracleDCN dem) {
        dcn = dem;
    }

    public void onDatabaseChangeNotification(DatabaseChangeEvent e) {

        System.out.println(e.toString());

        TableChangeDescription[] tcds = e.getTableChangeDescription();

        for (TableChangeDescription tcd: tcds) 
            System.out.println("Table changed: " + tcd.getTableName());

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

Solved by following workaround. Since I'm getting objectNumber in response, I use this number in this additional select to get table name:

select object_name from user_objects where object_id=73786

??? indicates a character set conversion error. "?" is the standard replacement character when an error occurs during conversion. If your table name has non ascii characters then maybe that explains why you don't see its name in the notification.

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