Oracle 10g Release2新功能之变化通知
欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入 引言 在.NET应用程序中,我们有很多方法实现对Oracle数据库的访问。 但是从功能和性能上来分析,Oracle Data Provider for .NET(ODP.NET)无疑是我们最好的选择,它是Oracle专门为基于.NET的应用程
欢迎进入Oracle社区论坛,与200万技术人员互动交流 >>进入
引言
在.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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Early this morning, Apple officially released iPadOS18. This system not only has the classic functions of iOS18, but also adds some unique functions, such as supporting mathematical note calculators, etc., which further improves the experience of iPad users. Friends who are interested Come and take a look. This time iPadOS18 not only perfectly inherits the core functions of iOS18, such as the personalized control center design, which allows users to freely adjust the order and layout of control items according to personal preferences, and the highly anticipated game mode, providing gamers with smoother and more The immersive gaming experience also incorporates a number of unique features specifically targeting the iPad’s large screen advantages and the creative uses of Apple Pencil, further expanding the iPad’s productivity.

The retention period of Oracle database logs depends on the log type and configuration, including: Redo logs: determined by the maximum size configured with the "LOG_ARCHIVE_DEST" parameter. Archived redo logs: Determined by the maximum size configured by the "DB_RECOVERY_FILE_DEST_SIZE" parameter. Online redo logs: not archived, lost when the database is restarted, and the retention period is consistent with the instance running time. Audit log: Configured by the "AUDIT_TRAIL" parameter, retained for 30 days by default.

The function in Oracle to calculate the number of days between two dates is DATEDIFF(). The specific usage is as follows: Specify the time interval unit: interval (such as day, month, year) Specify two date values: date1 and date2DATEDIFF(interval, date1, date2) Return the difference in days

The Oracle database startup sequence is: 1. Check the preconditions; 2. Start the listener; 3. Start the database instance; 4. Wait for the database to open; 5. Connect to the database; 6. Verify the database status; 7. Enable the service (if necessary ); 8. Test the connection.

The INTERVAL data type in Oracle is used to represent time intervals. The syntax is INTERVAL <precision> <unit>. You can use addition, subtraction, multiplication and division operations to operate INTERVAL, which is suitable for scenarios such as storing time data and calculating date differences.

To find the number of occurrences of a character in Oracle, perform the following steps: Get the total length of a string; Get the length of the substring in which a character occurs; Count the number of occurrences of a character by subtracting the substring length from the total length.

Oracle database server hardware configuration requirements: Processor: multi-core, with a main frequency of at least 2.5 GHz. For large databases, 32 cores or more are recommended. Memory: At least 8GB for small databases, 16-64GB for medium sizes, up to 512GB or more for large databases or heavy workloads. Storage: SSD or NVMe disks, RAID arrays for redundancy and performance. Network: High-speed network (10GbE or higher), dedicated network card, low-latency network. Others: Stable power supply, redundant components, compatible operating system and software, heat dissipation and cooling system.

The amount of memory required by Oracle depends on database size, activity level, and required performance level: for storing data buffers, index buffers, executing SQL statements, and managing the data dictionary cache. The exact amount is affected by database size, activity level, and required performance level. Best practices include setting the appropriate SGA size, sizing SGA components, using AMM, and monitoring memory usage.
