OCILIB Tutorials v5.0.0
Advanceddemo/notification.cdemo/notification.cpp

Database change notification — Subscription & Event

Objectives — by the end of this page you will be able to:

The subscription and the event

Continuous Query Notification lets the database push events to your app when data changes — no polling.

A Subscription is a registered interest: you subscribe on a connection, attach statements to scope it, and your handler fires when the watched objects change.

Each firing hands the handler an Event — a transient record (like the error object) telling you what changed.

Two environment prerequisites: the events flag at creation, and a subscription port for callbacks.

void event_handler(OCI_Event *event)
{
    printf("type=%u db=%s obj=%s rowid=%s\n",
           OCI_EventGetType(event), OCI_EventGetDatabase(event),
           OCI_EventGetObject(event), OCI_EventGetRowid(event));
}

OCI_Environment *env = OCI_EnvironmentCreate(err, NULL, OCI_ENV_EVENTS);
/* … connect … */
OCI_EnvironmentSetSubscriptionPort(env, 5468);

OCI_Subscription *sub = OCI_SubscriptionCreate(
    con, OTEXT("sub-00"), OCI_CNT_ALL, event_handler, OCI_SRM_DEFAULT, 0);

OCI_StatementPrepare(st, OTEXT("select * from employees"));
OCI_SubscriptionAddStatement(sub, st);       /* watch objects this stmt touches */

/* … when done … */
OCI_SubscriptionUnregister(sub);

What an event carries

C function C++ method Returns
OCI_EventGetType Event::GetType() OBJECT_CHANGED/STARTUP/SHUTDOWN/DROP_DATABASE/DEREGISTER
OCI_EventGetDatabase GetDatabase() db that changed
OCI_EventGetObject GetObject() table that changed
OCI_EventGetRowid GetRowid() the changed row (with rows-level)
OCI_EventGetOperation GetOperation() insert/update/delete/alter flag
OCI_EventGetSubscription GetSubscription() owning subscription

Notification levels

C enum C++ enum Granularity
OCI_CNT_OBJECTS Subscription::NotifyObjects object-level: "employees changed"
OCI_CNT_ROWS Subscription::NotifyRows row-level: which ROWIDs changed
OCI_CNT_DATABASES Subscription::NotifyDatabases db-level events (startup/shutdown/HA)
OCI_CNT_ALL Subscription::NotifyAll everything

The secure-notification mode flag (19.4+) requests client-initiated secure notification — wide builds hit a known Oracle client bug on it; MBCS builds are fine.

Subscription management

Putting it together — the typical sequence

Database change notification is a subscription + callback model:

  1. Create the subscription — OCI_SubscriptionCreate(con, name, type, callback) for query-change or object-change events.
  2. Register statements — StatementRegister on the queries you want watched.
  3. Get callbacks — your handler fires when the data changes; OCI_Event tells you what.
  4. Unregister — SubscriptionUnregister cleans up server-side.

Hints: the environment needs OCI_ENV_EVENTS at creation for notifications to work; the callback runs on a notification thread — don't block it; and Register is per-statement — you watch specific queries, not "the table".

Try it yourself

  1. Register OCI_CNT_ROWS on employees, then update a row from SQL*Plus — watch the event arrive.
  2. Subscribe with OCI_CNT_DATABASES and bounce a test instance (if you can).
  3. Add a second statement and confirm both tables' events fire.

Common pitfalls

Recap

Next: Checkpoint 6 — a job queue with workers and notifications.