2

In hexagonal architecture (or clean architecture), we have use cases that perform one or several interactions with infrastructure components.

These Use Cases are tipically transactional operations (we want them to be atomic).

So we would use the @Transactional annotation at Use Case method level.

@Component
@RequiredArgsConstructor
public class MyShinyUseCase implements MyShinyPrimaryPort {

    private final transactionalSecondaryPort trPort1; <--- Database 1
    private final anotherTransactionalSecondaryPort trPort2; <--- Database 2

    @Override
    @Transactional("UPS, THERE ARE 2 TRANSACTION MANAGERS IN THE CONTEXT")
    public void useCaseMethod() {
        trPort1.operate(); <--- Transactional resource
        trPort2.operate(); <--- Transactional resource, is error I want
                                to rollback the trPort1.operate().
    }

The problem comes when the Use Case interacts with more than one transactional resources, each of them with their TransactionManager, because the @Transactional annotation requires a specific TransactionManager to be specified, but the infrastructure specific TransactionManagers are infrastructure concerns: they should not be referenced from the domain layer, and even if we did, there are several TransactionManagers in play...

What would be the way to manage transactions involving several transactional resources in an hexagonal architecture?

Mr.Eddart
  • 10,050
  • 13
  • 49
  • 77

0 Answers0