What about database transactions?
We have not touched on the topic of database transactions yet. Where do we put our transaction boundaries?
A transaction should span all write operations to the database that are performed within a certain use case, ensuring that all those operations can be rolled back together if one of them fails.
Since the persistence adapter doesn’t know which other database operations are part of the same use case, it cannot decide when to open and close a transaction. We have to delegate this responsibility to the services that orchestrate the calls to the persistence adapter.
The easiest way to do this with Java and Spring is to add the @Transactional annotation to the domain service classes so that Spring will wrap all public methods with a transaction:
But doesn’t the @Transactional annotation introduce a dependency on a framework that we don’t want to have in our precious domain code? Well, yes, we...