Unit of Work
This pattern keeps track of the records an application operation needs to insert, update, or delete, then saves those changes together in a database transaction.
What it is
Placing an order might involve creating an order record and reducing the available stock. If the order is committed first and the stock update fails, the database contains only half the intended result. A Unit of Work (UoW) gives the application one place to coordinate the writes instead of letting each part of the operation save independently.
How it works
An operation starts with a shared persistence context. New and deleted objects are registered with it. For objects already loaded into that context, an ORM can detect changes automatically; another implementation might require explicit registration. When it is time to save, the UoW coordinates the writes, taking dependencies into account. For example, an order row may need to be inserted before the line items that reference it.
A Unit of Work tracks what needs to be written. A database transaction makes the writes atomic: they either commit together or are rolled back. In an ORM, flushing and committing are also distinct. A flush sends pending SQL to the database inside the current transaction; it does not make the transaction's changes permanent. An ORM may flush before the final commit, for example before a query that needs to see the pending changes.
Example: placing an order
with unit_of_work() as uow:
item = uow.inventory.get(product_id)
item.reserve(quantity)
uow.orders.add(Order(product_id, quantity))
uow.commit()
In this pseudocode, both repositories share a session. The session tracks the changed stock count and registers the new order. If a write fails, the context rolls back the uncommitted transaction and closes the session. That rollback protects the database; it does not necessarily restore the previous values of objects still held in memory.
Where it helps and what to watch for
The pattern is useful when one application operation changes several entities, often through different repositories. Hibernate's Session and Entity Framework Core's DbContext already provide change tracking, so a custom UoW wrapper is optional. It can be useful when the application layer needs an explicit save boundary without depending directly on ORM APIs.
Avoid committing in each repository. Once one write has committed, a later rollback cannot undo it. A local database rollback also cannot unsend an email or reverse an external API call. If a message must be published reliably after a database change, consider a transactional outbox.
Interview questions
A common question is: “Which layer should commit when a use case calls two repositories?” The application use case defines the boundary: both repositories work in the same context, and the operation commits after the business steps succeed. Be ready to distinguish atomicity from concurrency control. A UoW does not automatically stop two requests from reserving the same stock; that requires a separate safeguard such as optimistic locking.