oracle12c 监控表状态,类似触发器,获取表名称乱码问题

1、类似触发器原理,实时监听

2、解决获取表名称乱码问题

进入调试模式查看源码里面这个类,oracle tableName的编码模式:

oracle12c 监控表状态,类似触发器,获取表名称乱码问题_第1张图片

主体代码如下:搞了两天终于发现问题所在,tablename 开始出来是???这种乱码。确定是字符集编码的问题,在网上找了类似问题。需要引入oracle的语言包。

@Slf4j
public class MyTest {

    public static void main(String[] args) throws SQLException {
        String tableName = "tb_name";
        OracleDataSource dataSource = new OracleDataSource();
        dataSource.setUser("user");
        dataSource.setPassword("userPwd");
        dataSource.setURL("jdbc:oracle:thin:@localhost:1521/orcl");
        final OracleConnection conn = (OracleConnection) dataSource.getConnection();
        Properties prop = new Properties();
        // prop.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION, "true");
        // prop.setProperty(OracleConnection.DCN_NOTIFY_CHANGELAG, "1");
        prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
        //在第一次通知后,注册就作废撤销
        //prop.setProperty(OracleConnection.NTF_QOS_PURGE_ON_NTFN, "true");
        prop.setProperty(OracleConnection.NTF_QOS_PURGE_ON_NTFN, "false");
        prop.setProperty(OracleConnection.NTF_TIMEOUT, "0");
        final DatabaseChangeRegistration databaseChangeRegistration = conn.registerDatabaseChangeNotification(prop);
        databaseChangeRegistration.addListener(new DatabaseChangeListener() {
            @Override
            public void onDatabaseChangeNotification(DatabaseChangeEvent databaseChangeEvent) {
                long regId = databaseChangeEvent.getRegId();
                System.out.println("change notify: " + Arrays.toString(databaseChangeEvent.getTableChangeDescription()));
                if (regId == databaseChangeRegistration.getRegId()) {
                    TableChangeDescription[] tds = databaseChangeEvent.getTableChangeDescription();
                    System.out.println("===================================>>>");
                    System.out.println("'TableChangeDescription change count:" + tds.length);
                    for (TableChangeDescription td : tds) {
                        System.out.println("table id: " + td.getObjectNumber());
                        System.out.println("table name: " + td.getTableName());
                        RowChangeDescription[] rds = td.getRowChangeDescription();
                        for (RowChangeDescription rd : rds) {
                            //获取操作的类型
                            if (rd.getRowOperation().equals(RowChangeDescription.RowOperation.INSERT)) {
                                log.info("监控表:{} 变动", tableName);
                                log.info("本次监听的系统时间:" + DateUtil.getTime());
                                log.info("监控表:{} inset操作", tableName);
                            }
                            log.info("row id:{}", rd.getRowid().stringValue());
                            log.info("row change count:{}", rd.getRowOperation().toString());
                        }
                    }
                }
            }
        });
        OracleStatement statement = (OracleStatement) conn.createStatement();
        statement.setDatabaseChangeRegistration(databaseChangeRegistration);
        ResultSet resultSet = statement.executeQuery("select * from " + tableName + " where 1=2");
        statement.close();
        System.out.println("started");
    }
}

加入语言包:


     com.oracle.database.nls
     orai18n
     19.7.0.0

运行结果:

14:44:05.461 [Thread-5] INFO  c.l.c.t.d.n.OracleDCN - [onDatabaseChangeNotification,82] - 本次监听的系统时间:2024-12-29 14:44:05
14:44:05.461 [Thread-5] INFO  c.l.c.t.d.n.OracleDCN - [onDatabaseChangeNotification,83] - 监控表:DB_NAME.TB_TEST inset操作

你可能感兴趣的:(Java,java,oracle)