{"id":2494,"date":"2012-08-28T19:56:52","date_gmt":"2012-08-29T00:56:52","guid":{"rendered":"http:\/\/appcrawler.com\/wordpress\/?p=2494"},"modified":"2012-10-03T21:02:53","modified_gmt":"2012-10-04T02:02:53","slug":"jdbc-and-oracle-database-change-notification","status":"publish","type":"post","link":"http:\/\/appcrawler.com\/wordpress\/2012\/08\/28\/jdbc-and-oracle-database-change-notification\/","title":{"rendered":"JDBC and Oracle Database Change Notification"},"content":{"rendered":"<p>Below is a simple example of a cutdown example of using DCN in Oracle with JDBC.<\/p>\n<p>I plan on presenting this to our developers as an alternative to the ATG InventoryCache manager module.<\/p>\n<pre lang=\"java\" line=\"1\">\r\nimport java.sql.*;\r\nimport java.util.*;\r\nimport oracle.jdbc.*;\r\nimport oracle.jdbc.dcn.*;\r\nimport oracle.jdbc.dcn.RowChangeDescription.*;\r\n\r\npublic class ATGInventoryChangeNotify {\r\n  String URL = \"jdbc:oracle:thin:test\/test@192.168.1.50:1521\/express.home\";\r\n  Properties prop;\r\n\r\n  public static void main(String[] argv) {\r\n\r\n    ATGInventoryChangeNotify dcn = new ATGInventoryChangeNotify();\r\n    try {\r\n      dcn.prop = new Properties();\r\n      dcn.run();\r\n    }\r\n    catch(Exception e) {\r\n      e.printStackTrace();\r\n    }\r\n  }\r\n\r\n  void run() throws SQLException {\r\n    OracleConnection conn = (OracleConnection)DriverManager.getConnection(URL,prop);\r\n\r\n    Properties prop = new Properties();\r\n    prop.setProperty(OracleConnection.DCN_NOTIFY_ROWIDS,\"true\");\r\n    DatabaseChangeRegistration dcr = conn.registerDatabaseChangeNotification(prop);\r\n\r\n    try {\r\n      dcnListener list = new dcnListener(this);\r\n      dcr.addListener(list);\r\n\r\n      Statement stmt = conn.createStatement();\r\n      ((OracleStatement)stmt).setDatabaseChangeRegistration(dcr);\r\n      ResultSet rs = stmt.executeQuery(\"select * from dept where 1 = 2\");\r\n      rs.close();\r\n      stmt.close();\r\n    }\r\n    catch(Exception e) {\r\n      \/\/clean up our registration\r\n      if(conn != null)\r\n        conn.unregisterDatabaseChangeNotification(dcr);\r\n      e.printStackTrace();\r\n    }\r\n    finally {\r\n      try {\r\n        conn.close();\r\n      }\r\n      catch(Exception e){\r\n        e.printStackTrace();\r\n      }\r\n    }\r\n\r\n    try {\r\n      Thread.currentThread().join();\r\n    }\r\n    catch (Exception e) {\r\n      e.printStackTrace();\r\n    }\r\n\r\n    finally {\r\n      OracleConnection conn3 = (OracleConnection)DriverManager.getConnection(URL,prop);\r\n      conn3.unregisterDatabaseChangeNotification(dcr);\r\n      conn3.close();\r\n    }\r\n  }\r\n}\r\n\r\nclass dcnListener implements DatabaseChangeListener {\r\n  ATGInventoryChangeNotify dcn;\r\n  dcnListener(ATGInventoryChangeNotify dem) {\r\n    dcn = dem;\r\n  }\r\n\r\n  public void onDatabaseChangeNotification(DatabaseChangeEvent e) {\r\n    TableChangeDescription[] tc = e.getTableChangeDescription();\r\n\r\n    for (int i = 0; i < tc.length; i++) {\r\n      RowChangeDescription[] rcds = tc[i].getRowChangeDescription();\r\n      for (int j = 0; j < rcds.length; j++) {\r\n    \tSystem.out.println(rcds[j].getRowOperation() + \" \" + rcds[j].getRowid().stringValue());\r\n\t  }\r\n\t}\r\n    synchronized( dcn ){\r\n      dcn.notify();\r\n    }\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Below is a simple example of a cutdown example of using DCN in Oracle with JDBC. I plan on presenting this to our developers as an alternative to the ATG InventoryCache manager module. import java.sql.*; import java.util.*; import oracle.jdbc.*; import&hellip;<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"http:\/\/appcrawler.com\/wordpress\/2012\/08\/28\/jdbc-and-oracle-database-change-notification\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"footnotes":""},"categories":[19,24,25,22],"tags":[],"_links":{"self":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/2494"}],"collection":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2494"}],"version-history":[{"count":4,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/2494\/revisions"}],"predecessor-version":[{"id":2512,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/posts\/2494\/revisions\/2512"}],"wp:attachment":[{"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2494"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/appcrawler.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}