libpqxx: Notifications and Receivers

libpqxx 4.0.1

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

Notifications and Receivers

"Observer" base class for notifications. More...

"Observer" base class for notifications.

To listen on a notification issued using the NOTIFY command, derive your own class from notification_receiver and define its function-call operator to perform whatever action you wish to take when the given notification arrives. Then create an object of that class and pass it to your connection. DO NOT use raw SQL to listen for notifications, or your attempts to listen won't be resumed when a connection fails–and you'll have no way to notice.

Notifications never arrive inside a transaction, not even in a nontransaction. Therefore, you are free to open a transaction of your own inside your receiver's function invocation operator.

Notifications you are listening for may arrive anywhere within libpqxx code, but be aware that PostgreSQL defers notifications occurring inside transactions. (This was done for excellent reasons; just think about what happens if the transaction where you happen to handle an incoming notification is later rolled back for other reasons). So if you're keeping a transaction open, don't expect any of your receivers on the same connection to be notified.

(For very similar reasons, outgoing notifications are also not sent until the transaction that sends them commits.)

Multiple receivers on the same connection may listen on a notification of the same name. An incoming notification is processed by invoking all receivers (zero or more) of the same name.