使用oracle 12c更改通知问题

OracleCommand cmd = new OracleCommand("select * from Test WHERE TestFLAG = 1 or TestFLAGis not null", con); 

当表格发生变化时,无论条件是什么,我的.net项目仍会收到通知。

对于第二个问题,在我第一次收到任何通知后,表格上的任何更改都没有得到通知。 为什么?

解决我的问题的任何方法?

 public class MyNotificationSample { static string constr = "your db INFO"; public static bool IsNotified = false; static OracleDependency dep = null; public static void Main(string[] args) { //To Run this sample, make sure that the change notification privilege //is granted to scott. OracleConnection con = null; try { con = new OracleConnection(constr); OracleCommand cmd = new OracleCommand("select * from Test WHERE TestFLAG = 1 or TestFLAGis not null", con); con.Open(); // Set the port number for the listener to listen for the notification // request OracleDependency.Port = 1005; // Create an OracleDependency instance and bind it to an OracleCommand // instance. // When an OracleDependency instance is bound to an OracleCommand // instance, an OracleNotificationRequest is created and is set in the // OracleCommand's Notification property. This indicates subsequent // execution of command will register the notification. // By default, the notification request is using the Database Change // Notification. dep = new OracleDependency(cmd); // Add the event handler to handle the notification. The // OnMyNotification method will be invoked when a notification message // is received from the database dep.OnChange += OnMyNotificaton; // The notification registration is created and the query result sets // associated with the command can be invalidated when there is a // change. When the first notification registration occurs, the // notification listener is started and the listener port number // will be 1005. cmd.ExecuteNonQuery(); } catch (Exception e) { Console.WriteLine(e.Message); } con.Close(); Console.Write("Press Any Key to Quit"); Console.ReadLine(); // Loop while waiting for notification } public static void OnMyNotificaton(object src, OracleNotificationEventArgs arg) { if (dep.HasChanges) { Console.WriteLine("Notification Received"); DataTable changeDetails = arg.Details; Console.WriteLine("Data has changed in {0}", changeDetails.Rows[0]["ResourceName"]); } } 

最新更新 :让听众永不过期。

 new OracleDependency(cmd, false, 0 , true); 

但是,我的查询仍然无法正常工作……

将此添加到您的代码中

cmd.Notification.IsNotifiedOnce = false;

您的查询具有此WHERE子句: TestFLAG = 1 or TestFLAGis not null
TestFLAG之间可能缺少空格而且is not null 。 在这种情况下,表达式的第一部分是不必要的,因为当TestFLAG = 1 ,它不是null。
也许问题是,您的查询涵盖的内容远远超出您的预期。

除此之外,Oracle的数据库更改通知function并不保证您只会获得查询实际返回的行的通知。 它保证您将获得这些行的通知,但您也可以获得“误报”,因此通知行实际上与您的查询匹配。

这可能是Oracle Docs的一个很好的解释(强调我的):

基于查询的注册有两种模式:保证模式和尽力模式。 在保证模式下,任何数据库更改通知都可确保查询结果集中包含的内容发生更改。 但是,如果查询很复杂,则无法在保证模式下注册。 在这种情况下使用尽力而为模式。

尽力而为模式简化了基于查询的注册查询。 简化不会丢失任何通知。 但是,简化可能会导致误报 ,因为较简单版本的查询结果可能会在原始查询结果不会发生变化时。对于哪些查询可以使用尽力模式查询的注册仍然存在一些限制。 在这种情况下,开发人员可以使用基于对象的注册,这可以注册大多数查询类型。 基于对象的注册会在查询对象更改时生成通知,即使实际查询结果没有。 这也意味着基于对象的注册比基于查询的注册更容易出现误报。 开发人员应了解每个数据库更改通知选项的相对优势和劣势,并选择最适合其要求的选项。

在第二个问题上,如@ user1415516所写,您需要将通知设置为在第一次通知后未注册,因此请添加cmd.Notification.IsNotifiedOnce = false; 在执行命令之前。