We are building a multi-tenant server with a database/schema per tenant.
To do this we use Springs AbstractRoutingDataSource to switch the data source based on the tenant.
The tenant is determined by the sub domain of the url in a servlet filter.
This tenant is stored in a thread-local.
This all works fine until a thread is started.
For example when hibernate search re-indexes it starts a lot threads.
When one of these threads tries to get a connection the thread local returns null and I have no way to determine the tenant.
I fixed it by using a session scoped spring bean which stores my tenant.
See link for sample