Skip to content

Transactions and unit of work

Unit-of-work transactions are now documented as a shared Chronicle workflow with synchronized client examples.

Alongside unitOfWork.getAppendResults() (the full per-event detail from the latest commit), IUnitOfWork exposes three purpose-built accessors — thin filters over the same append results, so you don’t have to filter getAppendResults() yourself. This excerpt assumes a unitOfWork you started with store.unitOfWorkManager.begin():

  • getConstraintViolations() — every constraint violation across the commit.
  • getConcurrencyViolations() — every concurrency violation across the commit.
  • getAppendErrors() — every append error across the commit.
await unitOfWork.commit();
const constraintViolations = unitOfWork.getConstraintViolations();
const concurrencyViolations = unitOfWork.getConcurrencyViolations();
const appendErrors = unitOfWork.getAppendErrors();

transactional.append, transactional.appendMany and unitOfWork.addEvent accept namedTags beside the event source routing options. Tags are validated when the event is added, so an invalid tag throws InvalidNamedTag before anything is committed.

A commit can combine non-empty named tags and registered eventSource entries, on the same or on different entries and sequences, and every event keeps its event source. This needs Chronicle kernel 19.37.4 or later; older kernels drop the registered event source in such a batch (kernel issue). The client throws NamedTagsWithRegisteredEventSourceNotSupported before writing any group when the kernel version is older, unknown, or unparseable. With skipCompatibilityCheck=true the version is unknown, so this combination also throws. Commits without this combination are unaffected.

transactional.append() and transactional.appendMany() accept the per-event metadata of an ordinary append: registered eventSource/eventStream routing, the stream type, stream identifier and source type (as streamType/streamId/sourceType or eventStreamType/eventStreamId/eventSourceType), subject, occurred, tags, namedTags and a concurrencyScope. The unit of work keeps the values with each event (tags and the occurrence time are copied when you enroll the event) and passes them to the append when you commit, in the order you enrolled the events.

const unitOfWork = store.unitOfWorkManager.begin();
await store.eventLog.transactional.append('order-123', new TransactionalOrderPlaced('order-123', 99.95), {
eventStreamType: 'Orders',
eventStreamId: 'order-123',
subject: 'customer-42',
occurred: new Date('2026-03-04T05:06:07Z'),
tags: ['priority'],
concurrencyScope: { sequenceNumber: expectedOrderRevision, eventSourceId: true }
});
await unitOfWork.commit();
  • Chronicle validates one concurrency scope per event source identifier in an append, so every event a unit of work holds for the same event source identifier in the same event sequence must carry an equal scope or none. A different scope throws ConflictingConcurrencyScopesInUnitOfWork when you enroll the event, and the unit of work keeps the events it already had.
  • Giving both names of a pair with different values throws ConflictingAppendOptionNamesInUnitOfWork when you enroll the event.
  • correlationId, concurrencyScopes and eventSourceId throw AppendOptionNotSupportedInUnitOfWork. The unit of work uses its own correlation identifier (pass one to unitOfWorkManager.begin(correlationId)) and per-event concurrencyScopes. In both cases the unit of work keeps the events it already had.
  • A unit of work commits one append per event sequence. Events in the same event sequence are appended as one batch; events in different event sequences are separate appends, so a failure in a later sequence does not undo an earlier one.
  • A rejected append, such as a stale concurrency scope, shows up in getConcurrencyViolations() and isSuccess after the commit.