What about Database Transactions?
We have not touched upon 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 so 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 application service classes so that Spring will wrap all public methods with a transaction:
package buckpal.application.service;
@Transactional
public class SendMoneyService implements SendMoneyUseCase {
...
}
If we want our services to stay pure and not be stained with @Transactional annotations...