jdbc:oracle数据库更改通知和重复事件

对于Oracle数据库表的任何更改(更新,插入,删除),我都需要一些侦听器。

问题:通过对表进行一次更新,我得到了很多检测。

我认为它的oracle缓存等

是否有可能仅检测到真正的变化?

我的代码:

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 OracleDCN {

static final String USERNAME = "scott";

static final String PASSWORD = "tiger";

static String URL = "jdbc:oracle:thin:@localhost:1521:stingdev";

public static void main(String[] args) {

OracleDCN oracleDCN = new OracleDCN();

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("Changed row id : "+dce.getTableChangeDescription()[0].getRowChangeDescription()[0].getRowid().stringValue());

}

});

Statement stmt = conn.createStatement();

((OracleStatement) stmt).setDatabaseChangeRegistration(dcr);

ResultSet rs = stmt.executeQuery("select * from EXAMPLE where ID=1");

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", OracleDCN.USERNAME);

prop.setProperty("password", OracleDCN.PASSWORD);

return (OracleConnection) dr.connect(OracleDCN.URL, prop);

}

}

输出:

Changed row id : AAAFSzAAAAAAAG8AAA

Changed row id : AAAFSzAAAAAAAG8AAA

Changed row id : AAAFSzAAAAAAAG8AAA

Changed row id : AAAFSzAAAAAAAG8AAA

回答:

问题是我多次注册到数据库。解决方案是在注册之前检查注册用户是否有更改事件。

我通过以下查询进行了检查:

select TABLE_NAME 

from USER_CHANGE_NOTIFICATION_REGS

以上是 jdbc:oracle数据库更改通知和重复事件 的全部内容, 来源链接: utcz.com/qa/403923.html

回到顶部