标签:oracle otl data change notifica ora-24912 源码 实现
#include "stdafx.h" #include "ocilib.h" #pragma comment (lib, "ociliba.lib") #ifdef _WINDOWS #define sleep(x) Sleep(x*1000) #endif #define wait_for_events() sleep(5000) void event_handler(OCI_Event *event); void error_handler(OCI_Error *err); int main(void) { OCI_Connection *con; OCI_Subscription *sub; OCI_Statement *st; printf("=> Initializing OCILIB in event mode...\n\n"); //0.第二个参数为原有的oracle的DLL所在的路径名称。 if (!OCI_Initialize(error_handler, "oracle", OCI_ENV_EVENTS)) return EXIT_FAILURE; printf("=> Connecting to usr@db...\n\n"); //1.连接 第一个参数格式:【IP:端口/服务名】,第二个参数:登录用户名,第三个参数:密码。 con = OCI_ConnectionCreate("100.200.10.50:1521/ts54", "tss54", "psdts**", OCI_SESSION_DEFAULT); OCI_SetAutoCommit(con, TRUE); printf("=> Creating statement...\n\n"); st = OCI_StatementCreate(con); printf("=> Creating tables...\n\n"); //2.创建数据表 OCI_ExecuteStmt(st, "create table table1(code number)"); OCI_ExecuteStmt(st, "create table table2(str varchar2(10))"); printf("=> Registering subscription...\n\n"); //3.注册通知事件 sub-00 为通知名称. sub = OCI_SubscriptionRegister(con, "sub-00", OCI_CNT_ALL, event_handler, 5468, 0); printf("=> Adding queries to be notified...\n\n"); OCI_Prepare(st, "select * from table1"); OCI_SubscriptionAddStatement(sub, st); OCI_Prepare(st, "select * from table2"); OCI_SubscriptionAddStatement(sub, st); //等待响应注册事件 wait_for_events(); //可以Sleep足够时间一直等待,当有其他进程进行修改表的操作,该处就会有响应。 // 以下为测试用,可以不用执行。 // #if 0 // //4.执行对应数据库alter操作 // printf("=> Executing some DDL operation...\n\n"); // OCI_ExecuteStmt(st, "alter table table1 add price number"); //alter事件 // //等待5s,等待打印输出. // wait_for_events(); // // // //5.执行数据库的inser,update操作。 // printf("=> Executing some DML operation...\n\n"); // OCI_ExecuteStmt(st, "insert into table1 values(1, 10.5)"); // OCI_ExecuteStmt(st, "insert into table2 values('shoes')"); // OCI_ExecuteStmt(st, "update table1 set price = 13.5 where code = 1"); // OCI_ExecuteStmt(st, "delete from table2 "); // wait_for_events(); // // //6.执行drop数据库表操作。 // printf("=> Droping tables...\n\n"); // OCI_ExecuteStmt(st, "drop table table1"); // OCI_ExecuteStmt(st, "drop table table2"); // wait_for_events(); // // printf("=> Disconnecting from DB...\n\n"); // OCI_ConnectionFree(con); // printf("=> Stopping the remote database...\n\n"); // OCI_DatabaseShutdown("db", "sys", "sys", // OCI_SESSION_SYSDBA, // OCI_DB_SDM_FULL, // OCI_DB_SDF_IMMEDIATE); // #endif printf("=> Unregistering subscription...\n\n"); OCI_SubscriptionUnregister(sub); printf("=> Cleaning up OCILIB resources...\n\n"); OCI_Cleanup(); printf("=> Done...\n\n"); return EXIT_SUCCESS; } void error_handler(OCI_Error *err) { int err_type = OCI_ErrorGetType(err); const char *err_msg = OCI_ErrorGetString(err); printf("** %s - %s\n", err_type == OCI_ERR_WARNING ? "Warning" : "Error", err_msg); } void event_handler(OCI_Event *event) { unsigned int type = OCI_EventGetType(event); unsigned int op = OCI_EventGetOperation(event); OCI_Subscription *sub = OCI_EventGetSubscription(event); printf("** Notification : %s\n\n", OCI_SubscriptionGetName(sub)); printf("...... Database : %s\n", OCI_EventGetDatabase(event)); switch (type) { case OCI_ENT_STARTUP: printf("...... Event : Startup\n"); break; case OCI_ENT_SHUTDOWN: printf("...... Event : Shutdown\n"); break; case OCI_ENT_SHUTDOWN_ANY: printf("...... Event : Shutdown any\n"); break; case OCI_ENT_DROP_DATABASE: printf("...... Event : drop database\n"); break; case OCI_ENT_DEREGISTER: printf("...... Event : deregister\n"); break; case OCI_ENT_OBJECT_CHANGED: printf("...... Event : object changed\n"); printf("........... Object : %s\n", OCI_EventGetObject(event)); switch (op) { case OCI_ONT_INSERT: printf("........... Action : insert\n"); break; case OCI_ONT_UPDATE: printf("........... Action : update\n"); break; case OCI_ONT_DELETE: printf("........... Action : delete\n"); break; case OCI_ONT_ALTER: printf("........... Action : alter\n"); break; case OCI_ONT_DROP: printf("........... Action : drop\n"); break; } if (op < OCI_ONT_ALTER) printf("........... Rowid : %s\n", OCI_EventGetRowid(event)); break; } printf("\n"); }
作者:铭毅天下
转载请标明出处,原文地址:http://blog.csdn.net/laoyang360/article/details/46524519
如果感觉本文对您有帮助,请点击‘顶’支持一下,您的支持是我坚持写作最大的动力,谢谢!
深入详解Oracle data change notification
标签:oracle otl data change notifica ora-24912 源码 实现
原文地址:http://blog.csdn.net/laoyang360/article/details/46524519