简体   繁体   English

Java - Oracle 数据库更改通知

[英]Java - Oracle Database Change Notification

I am trying to implement a event listener which can identify DATABASE CHANGE NOTIFICATION (Oracle).我正在尝试实现一个可以识别数据库更改通知(Oracle) 的事件侦听器。 According to the reference website , it said that event will triggle and print ROW_ID when something change in EXAMPLE table.根据参考网站,它说当EXAMPLE表中的某些内容发生变化时,事件将触发并打印ROW_ID。 I want this project running, and it should give me a message "give me something!"我想让这个项目运行,它应该给我一条消息“给我一些东西!” if I manually insert/update data in Database.如果我在数据库中手动插入/更新数据。 However, it is my understanding that this code will terminate no matter what since there is no infinite loop that can be interrupted by the event.但是,我的理解是该代码无论如何都会终止,因为没有可以被事件中断的无限循环。 Please correct me if I am wrong.如果我错了,请纠正我。

Additional Question]补充问题]
By setting OracleConnection.DCN_NOTIFY_ROWIDS as true, it will notify every event including inserting, updating, deleting.通过将OracleConnection.DCN_NOTIFY_ROWIDS设置为 true,它将通知每个事件,包括插入、更新、删除。 Am I correct?我对么? I was confused with the meaning of "Database change events will include row-level details, such as operation type and ROWID"我对“数据库更改事件将包括行级详细信息,例如操作类型和 ROWID”的含义感到困惑

my code:我的代码:

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;
import oracle.jdbc.OracleConnection;
import oracle.jdbc.OracleDriver;
import oracle.jdbc.OracleStatement;
import oracle.jdbc.dcn.DatabaseChangeEvent;
import oracle.jdbc.dcn.DatabaseChangeListener;
import oracle.jdbc.dcn.DatabaseChangeRegistration;

public class DBTest {
    static final String USERNAME = "username";
    static final String PASSWORD = "password";
    static String URL = "jdbc:oracle:thin:@url:port/name";

    public static void main(String[] args) {
        DBTest oracleDCN = new DBTest();
        try {
            oracleDCN.run();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private void run() throws Exception {
        OracleConnection conn = connect();
        Properties prop = new Properties();
        prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
        DatabaseChangeRegistration dcr = conn.registerDatabaseChangeNotification(prop);

        try {
            dcr.addListener(new DatabaseChangeListener() {
                public void onDatabaseChangeNotification(DatabaseChangeEvent dce) {
                    System.out.println("GIVE ME SOMETHING!");
                }
            });
            //conn.unregisterDatabaseChangeNotification(dcr);
            Statement stmt = conn.createStatement();
            ((OracleStatement) stmt).setDatabaseChangeRegistration(dcr);
            ResultSet rs = stmt.executeQuery("select * from Schema.T_TEST");
            while (rs.next()) {
            }
            rs.close();
            stmt.close();
        } catch (SQLException ex) {
            if (conn != null) {
                conn.unregisterDatabaseChangeNotification(dcr);
                conn.close();
            }
            throw ex;
        }
    }

    OracleConnection connect() throws SQLException {
        OracleDriver dr = new OracleDriver();
        Properties prop = new Properties();
        prop.setProperty("user", DBTest.USERNAME);
        prop.setProperty("password", DBTest.PASSWORD);
        return (OracleConnection) dr.connect(DBTest.URL, prop);
    }
}

More details can be found in the reference website可以在参考网站中找到更多详细信息

As you guessed you need keep your main thread alive otherwise your program will exit.正如您猜测的那样,您需要保持主线程处于活动状态,否则您的程序将退出。 You can just make it sleep or do something more useful.你可以让它睡觉或做一些更有用的事情。 Also you can close the JDBC connection but you don't want to close (unregister) the registration immediately.您也可以关闭 JDBC 连接,但您不想立即关闭(取消注册)注册。 The way Database Change Notification works is with an internal listening thread that runs within the driver.数据库更改通知的工作方式是使用在驱动程序中运行的内部侦听线程。 This listening thread will receive outband events sent by the server through a dedicated network socket, process these events and notify the listeners.该监听线程将通过专用网络套接字接收服务器发送的带外事件,处理这些事件并通知监听器。 This listening thread will be closed if you unregister.如果您取消注册,此监听线程将关闭。 When you're no longer interested in receiving these events you can create another connection to the database to unregister which will end up closing the driver's listening thread.当您不再对接收这些事件感兴趣时,您可以创建另一个到数据库的连接以取消注册,这最终会关闭驱动程序的侦听线程。

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

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