I am using Java 8 and using oracle version as the Oracle Database 12c Enterprise Edition Release 12.1.0.2.0.
We are using below driver to connect to the database.
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.11.0.0</version>
</dependency>
I have gone through the some of the articles related to implement the Database Change Notification and implemented the below code.
DBTest
public class DBTest {
private static final String USERNAME="username";
private static final String PASSWORD="password";
private static final String URL="jdbc:oracle:thin:@<hostname>:port:servicename";
public static void main(String[] args) throws SQLException {
DBTest dbTest=new DBTest();
dbTest.testDBChangeNotification();
}
private void testDBChangeNotification() throws SQLException {
System.out.println("Invoke the testDBChangeNotification Started .....");
Properties prop = new Properties();
prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS, "true");
OracleConnection conn = connect();
DatabaseChangeRegistration dcr = null;
try {
dcr = conn.registerDatabaseChangeNotification(prop);
DCNListener dcnListener = new DCNListener();
dcr.addListener(dcnListener);
Statement stmt = conn.createStatement();
((OracleStatement) stmt).setDatabaseChangeRegistration(dcr);
ResultSet rs = stmt.executeQuery("select * from Example");
while (rs.next()) {
}
rs.close();
stmt.close();
String[] tableNames = dcr.getTables();
Arrays.stream(tableNames)
.forEach(i ->System.out.println("Table " + i+ " registered." ));
}
catch(SQLException sqlException){
if (conn != null)
{
conn.unregisterDatabaseChangeNotification(dcr);
conn.close();
}
sqlException.printStackTrace();
throw sqlException;
}
System.out.println("Finished the testDBChangeNotification Started .....");
}
OracleConnection connect() throws SQLException {
OracleDriver dr = new OracleDriver();
Properties prop = new Properties();
prop.setProperty("user", DBTest.USERNAME);
prop.setProperty("password", DBTest.PASSWORD);
OracleConnection oracleConn = (OracleConnection) dr.connect(DBTest.URL, prop);
if(oracleConn!=null){
System.out.println("oracle connection is available");
}
return oracleConn;
}
}
DBListener
public class DCNListener implements DatabaseChangeListener {
@Override
public void onDatabaseChangeNotification(DatabaseChangeEvent databaseChangeEvent) {
System.out.println("Received the database change event notification");
TableChangeDescription[] tableChanges = databaseChangeEvent.getTableChangeDescription();
for (TableChangeDescription tableChange : tableChanges) {
RowChangeDescription[] rcds = tableChange.getRowChangeDescription();
for (RowChangeDescription rcd : rcds) {
RowChangeDescription.RowOperation op = rcd.getRowOperation();
String rowId = rcd.getRowid().stringValue();
System.out.println("Value of the rowId:" + rowId);
switch (op) {
case INSERT:
System.out.println("Insert the value of RowId: " + rowId);
break;
case UPDATE:
System.out.println("Update the value of RowId: " + rowId);
break;
}
}
}
System.out.println("Finished the database change event notification");
}
}
Issue with the above code is I can see the registration is successful, but I am still not able to receive the database change notification in my DCNListener when I am inserting and updating in the table from oracle SQL Developer and commiting the insert/update.
I just want to know what I am doing wrong in above code due to which I am not able to receive notifications when DataChanges