Transactional Event Publishing
If an on execute hook is linked to the PublishEvent, this hook is executed immediately within the user transaction (without enqueuing first). If io.default is set to true, the event is enqueued after executing the hook. The hook is not executed twice for that event.
Background:
It is not possible to first check the subscription and enqueue, because we don’t know what is coded in the hook. For example, the hook may write to a table, thus requiring a database transaction context (which the publisher process does not have). Or the hook may change the contents of the event before executing the default process, so we cannot check the subscription before invoking the hook. Or the hook may contain subscription-independent actions, so again we cannot check the subscription before invoking the hook.
This implies that a PublishEvent hook is not always executed at the same moment:
- If the application invokes PublishEvent directly, the hook is invoked inside the application transaction. (No queuing is done.)
- If the application invokes ShowAndPublish(Standard)Event, the event is first queued, so the PublishEvent is invoked asynchronously, outside the application transaction and application context.
Events from ShowAndPublishEvent and ShowAndPublishStandardEvent are also published through PublishEvent. However, in that case the publishing will immediately be done; it will not be enqueued and dequeued a second time.