表更改时未触发Oracle Change Notification

时间:2019-02-27 18:36:46

标签: oracle jdbc oracle12c change-notification

第一次尝试做这样的事情,我不确定我缺少什么吗?

我的代码:

package SQLOCpackage;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;    
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;    
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleStatement;
import oracle.jdbc.dcn.DatabaseChangeEvent;
import oracle.jdbc.dcn.DatabaseChangeListener;
import oracle.jdbc.dcn.DatabaseChangeRegistration;
import oracle.jdbc.dcn.RowChangeDescription;
import oracle.jdbc.dcn.TableChangeDescription;

@SuppressWarnings("serial")
public class SQLONframe extends JFrame {
String URL = "jdbc:oracle:thin:@xxxxx.xxxxx.xx.xxxx:1521:xxxxx";
Properties prop;
private JPanel contentPane;
static String PW                    = "xxxxxxxx";
static String UN                    = "xxxxxxxx";

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                SQLONframe frame = new SQLONframe();
                frame.setVisible(true);

                SQLONframe dcn = new SQLONframe();
                try {
                  dcn.prop = new Properties();
                  dcn.prop.setProperty("user", UN);
                  dcn.prop.setProperty("password", PW);
                  dcn.run();
                }
                catch(Exception e) {
                  e.printStackTrace();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

public SQLONframe() {
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setBounds(100, 100, 450, 300);
    contentPane = new JPanel();
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
    contentPane.setLayout(new BorderLayout(0, 0));
    setContentPane(contentPane);
}

void run() throws SQLException {
    OracleConnection conn = (OracleConnection)DriverManager.getConnection(URL,prop);
    DatabaseChangeRegistration dcr = null;
    Properties prop = new Properties();
    prop.setProperty(OracleConnection.DCN_QUERY_CHANGE_NOTIFICATION,"true");

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

      Statement stmt = conn.createStatement();
      ((OracleStatement)stmt).setDatabaseChangeRegistration(dcr);
      ResultSet rs = stmt.executeQuery("select script_name, current_status, Issues_found_during_run, Testers, tools from ALLDATA WHERE ID = 1");
      while (rs.next())
      {

          }

      String[] tableNames = dcr.getTables();
      for(int i=0;i<tableNames.length;i++)
        System.out.println(tableNames[i]+" is part of the registration.");

      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 {
    SQLONframe dcn;
  dcnListener(SQLONframe dem) {
    dcn = dem;
  }

  public void onDatabaseChangeNotification(DatabaseChangeEvent e) {
    TableChangeDescription[] tc = e.getTableChangeDescription();

    for (int i = 0; i < tc.length; i++) {
      RowChangeDescription[] rcds = tc[i].getRowChangeDescription();
      for (int j = 0; j < rcds.length; j++) {
        System.out.println(rcds[j].getRowOperation() + " " + rcds[j].getRowid().stringValue());
      }
    }
    synchronized( dcn ){
      dcn.notify();
    }
  }

}
 }

我可以看到它确实注册了变更通知,但是当我去更改该表中的某些内容并提交时,我从代码那边什么也没说改变了?

任何帮助都会很棒!

更新

使用

SELECT * from USER_CHANGE_NOTIFICATION_REGS 

在SQL Developer中执行该查询时,我确实获得了注册信息:

enter image description here

ojdbc8.jar的版本信息为:

  

使用javac 1.8.0_171 on

编译的Oracle 18.3.0.0.0 JDBC 4.2      

Tue_Jun_26_11:06:40_PDT_2018

     

默认连接属性资源

     

2019年3月12日星期二10:06:48

这是Oracle SQL Developer的状态:

  

Oracle Database 12c企业版12.2.0.1.0版-64位生产

     

PL / SQL版本12.2.0.1.0-生产

     

“ CORE 12.2.0.1.0生产”

     

TNS for Linux:版本12.2.0.1.0-正式版

     

NLSRTL版本12.2.0.1.0-生产

在我的程序运行并且查询已注册的情况下,这是我在端口上看到的内容:

enter image description here

0 个答案:

没有答案