简体   繁体   English

Canot通过Oracle数据库更改通知获取表名

[英]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. 我的数据库是Oracle11g,正在使用ojdbc6.jar jdbc驱动程序库。 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. 存在DatabaseChangeEvent中的所有其他数据,仅缺少表名。

I'm using this code bellow I found on Internet which I slightly modified to my needs. 我正在使用以下代码,该代码是在Internet上找到的,我对其稍作修改。

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: 由于我正在获取objectNumber作为响应,因此在此附加选择中使用此数字来获取表名称:

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. 如果您的表名包含非ASCII字符,那么也许可以解释为什么您在通知中看不到其名称。

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

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