Oracle 10g Release2新功能之變化通知

來源:互聯網
上載者:User

引言

在.NET應用程式中,我們有很多方法實現對Oracle資料庫的訪問。 但是從功能和效能上來分析,Oracle Data Provider for .NET(ODP.NET)無疑是我們最好的選擇,它是Oracle專門為基於.NET的應用程式設計的一套介面,它的訪問速度要遠遠快於其它方法。

本文將介紹Oracle Database的新功能之一:Change Notification。為了能更好的說明這個新功能,我將用常式的方式來介紹它的定義和使用方法。

Database Change Notification的產生背景

在現在的程式開發過程中,我們經常考慮的一個提高效能的方法就是用data cache。Data cache 避免了我們每次需要資料的時候都去訪問資料庫,這樣節省了大量的時間。但是這樣就出現了一個問題,當我們用data cache的時候,如果在資料庫中的資料發生了改變,那麼我們data cache 中的資料就和資料庫的資料不一致了,這樣將會導致錯誤。為瞭解決這個問題,我們一般常用兩種方法。

1. 讓使用者手動的更新data cache的內容,例如提供一個更新按鈕。

2. 讓我們的程式間隔一定的時間自動去更新data cache的內容。

不難看出,這兩種方法都有相當的局限性。

第一種方法使用者必須記住經常的去更新資料,如果資料庫中的資料改了但是使用者並沒有去更新資料,這樣將導致錯誤。第二種方法的局限性是我們沒有辦法設定一個恰好的時間間隔使資料庫資料變化的時候保證data cache的資料也發生變化。

Database Change Notification就是為瞭解決這個難題。

Database Change Notification 的基本概念

Database Change Notification的作用就是當資料庫中的資料發生變化的時候,自動發出一個通知。

用Database Change Notification有三個步驟:

1. 註冊: 指定資料庫要監聽的查詢。ODP.NET自動註冊基於這個查詢的監聽事件。資料庫可以監聽DML(Data Manipulation Language)事件,DDL(Data Definition Language)事件,和global 事件(例如關閉資料庫)。

2. 通知:一旦資料庫中的資料發生變化,資料庫將自動發出通知,我們要在我們的程式中定義事件處理操作。

3. 響應:在我們的程式中,一旦收到通知,我們一般情況下會自動更新data cache,當然我們可以通知使用者資料發生改變,由他來決定是否進行更新。

舉例:

在ODP.NET中使用Database Change Notification很簡單,請看下面的常式。這個常式用HR資料庫使用者。

static void Main(string[] args)
{
 string sql = "select first_name, last_name, salary from employees where employee_id = 149";
 string constr = "User Id=hr;Password=hr;Data Source=oramag;Pooling=false";
 OracleConnection con = new OracleConnection(constr);
 con.Open();
 OracleCommand cmd = new OracleCommand(sql, con);
 OracleDependency dep = new OracleDependency(cmd);
 dep.OnChange += new OnChangeEventHandler(OnDatabaseNotification);
 cmd.ExecuteNonQuery();
 while (notificationReceived == false)
 {
  Console.WriteLine("Waiting for notification...");
  System.Threading.Thread.Sleep(2000);
 }
 cmd.Dispose();
 con.Dispose();
 Console.WriteLine("Press ENTER to continue...");
 Console.ReadLine();
}
public static void OnDatabaseNotification(object src, OracleNotificationEventArgs args)
{
 Console.WriteLine("Database Change Notification received!");
 DataTable changeDetails = args.Details;
 Console.WriteLine("Resource {0} has changed.", changeDetails.Rows[0]["ResourceName"]);
 notificationReceived = true;
}

HR一定要有change notification 許可權,我們用下面的命令。

grant change notification to hr;

在你的電腦上安裝ODP.NET,添加下面的using statement在你的代碼剛開始的地方。

using System.Threading;
using System.Data;
using Oracle.DataAccess.Client;

現在你就可以運行這段常式了。輸出如下:

Waiting for notification...

這個時候去修改你的資料庫,例如用下面的命令,

update employees set salary = salary+10
where employee_id = 149;
commit;

可以看到有如下的輸出,

Database Change Notification received!
Resource HR.EMPLOYEES has changed.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.