Java - Oracle Database Change Notification

I am trying to implement an event listener that can identify DATABASE CHANGE NOTIFICATION (Oracle). According to the help site , he said the event will fire and print the ROW_ID when something changes in the EXAMPLE table. I want this project to work and it should give me a "give me something!" Message. if i manually insert / update data in the database. However, I understand that this code will complete regardless, since there is no infinite loop that can be interrupted by an event. Please correct me if I am wrong.

Additional question]
By setting OracleConnection.DCN_NOTIFY_ROWIDS as true, it will notify every event including insert, update, delete. I'm right? I was confused by the value "Database change events will include row-level data such as operation type and 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 information can be found on the website

+4


source to share


1 answer


As you may have guessed, you need to save your main thread, otherwise your program will exit. You can just fall asleep or do something more rewarding. Also you can close the JDBC connection but don't want to close the registration immediately. The way to work with database change notification is with an internal listening thread that runs inside the driver. This listening thread will receive output events sent by the server over a dedicated network socket, handle those events, and notify listeners. This listening thread will be closed if you unregister. When you are no longer interested in receiving these events, you can create another database connection to unregister, which will end up closing the driver's listening thread.



0


source







All Articles