I need to have a different database for each tenant in a web application.
A tenant is an organization.
A logged user (member of an organization) can send commands to interact with application.
Both CommandBus and EventBus should be asynchronous.
During the web request (therefore request scope), a command dispatch interceptor inject the tenant (gathered from the logged user) in the command message.
Then the commandBus dispatch the command to the correct handler.
At this point the request scope is destroyed and the execution goes on in a different thread.
Then the command handler handles the command and the aggregate publishes events.
Those events are handled each one in its own thread.
I’m using CDI and everything works pretty well in the case of a single eventstore.
But what about selecting eventstore at runtime taking into account the tenant information stored in the command message?
Is it better to store tenant information in the metadata or in the command payload?
Whereas in the command part I have a command interceptor for injecting the tenant information in the command, in event part I have found nothing similar.
Is there a way to automatically provide that information to the event message?
Maybe should the tenant information be stored in the UnitOfWork?
I also thinked about using a bound session scope (or a similar custom scope), but I can realize neither a correct lifespan nor a correct way to manage it.