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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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











Solutions to Oracle cannot be opened include: 1. Start the database service; 2. Start the listener; 3. Check port conflicts; 4. Set environment variables correctly; 5. Make sure the firewall or antivirus software does not block the connection; 6. Check whether the server is closed; 7. Use RMAN to recover corrupt files; 8. Check whether the TNS service name is correct; 9. Check network connection; 10. Reinstall Oracle software.

The method to solve the Oracle cursor closure problem includes: explicitly closing the cursor using the CLOSE statement. Declare the cursor in the FOR UPDATE clause so that it automatically closes after the scope is ended. Declare the cursor in the USING clause so that it automatically closes when the associated PL/SQL variable is closed. Use exception handling to ensure that the cursor is closed in any exception situation. Use the connection pool to automatically close the cursor. Disable automatic submission and delay cursor closing.

In Oracle, the FOR LOOP loop can create cursors dynamically. The steps are: 1. Define the cursor type; 2. Create the loop; 3. Create the cursor dynamically; 4. Execute the cursor; 5. Close the cursor. Example: A cursor can be created cycle-by-circuit to display the names and salaries of the top 10 employees.

Oracle views can be exported through the EXP utility: Log in to the Oracle database. Start the EXP utility, specifying the view name and export directory. Enter export parameters, including target mode, file format, and tablespace. Start exporting. Verify the export using the impdp utility.

When Oracle log files are full, the following solutions can be adopted: 1) Clean old log files; 2) Increase the log file size; 3) Increase the log file group; 4) Set up automatic log management; 5) Reinitialize the database. Before implementing any solution, it is recommended to back up the database to prevent data loss.

Building a Hadoop Distributed File System (HDFS) on a CentOS system requires multiple steps. This article provides a brief configuration guide. 1. Prepare to install JDK in the early stage: Install JavaDevelopmentKit (JDK) on all nodes, and the version must be compatible with Hadoop. The installation package can be downloaded from the Oracle official website. Environment variable configuration: Edit /etc/profile file, set Java and Hadoop environment variables, so that the system can find the installation path of JDK and Hadoop. 2. Security configuration: SSH password-free login to generate SSH key: Use the ssh-keygen command on each node

Oracle is not only a database company, but also a leader in cloud computing and ERP systems. 1. Oracle provides comprehensive solutions from database to cloud services and ERP systems. 2. OracleCloud challenges AWS and Azure, providing IaaS, PaaS and SaaS services. 3. Oracle's ERP systems such as E-BusinessSuite and FusionApplications help enterprises optimize operations.

To stop an Oracle database, perform the following steps: 1. Connect to the database; 2. Shutdown immediately; 3. Shutdown abort completely.
