Java-Oracle数据库更改通知 [英] Java - Oracle Database Change Notification

查看:285
本文介绍了Java-Oracle数据库更改通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现一个事件侦听器,该事件侦听器可以标识数据库更改通知(Oracle).根据 参考网站 ,它表示当EXAMPLE表中发生某些更改时,该事件将触发并打印ROW_ID.我希望该项目运行,并且应该给我一条消息给我点东西!"如果我在数据库中手动插入/更新数据.但是,据我了解,此代码无论如何都将终止,因为没有无限循环可以被事件中断.如果我错了,请纠正我.

I am trying to implement a event listener which can identify DATABASE CHANGE NOTIFICATION (Oracle). According to the reference website, it said that event will triggle and print ROW_ID when something change in EXAMPLE table. 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.

其他问题
通过将 OracleConnection.DCN_NOTIFY_ROWIDS 设置为true,它将通知每个事件,包括插入,更新,删除.我对么?我对数据库更改事件将包含行级详细信息,例如操作类型和ROWID"

Additional Question]
By setting OracleConnection.DCN_NOTIFY_ROWIDS as true, it will notify every event including inserting, updating, deleting. Am I correct? I was confused with the meaning of "Database change events will include row-level details, such as operation type and ROWID"

我的代码:

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

推荐答案

如您所料,您需要保持主线程处于活动状态,否则程序将退出.您可以让它入睡或做一些更有用的事情.另外,您可以关闭JDBC连接,但不想立即关闭(注销)注册.数据库更改通知的工作方式是在驱动程序内运行的内部侦听线程.该侦听线程将接收服务器通过专用网络套接字发送的带外事件,处理这些事件并通知侦听器.如果您取消注册,此监听线程将关闭.当您不再对接收这些事件感兴趣时,可以创建另一个数据库连接以注销,这将最终关闭驱动程序的侦听线程.

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. 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.

这篇关于Java-Oracle数据库更改通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆