Our web application sets multiple thread locals on every http request. Some of the thread locals are used by mapped entities when they get loaded by hibernate.
On initial reindexing we use massindexer. Massindexer creates multiple threads to do the job.
Because the http request thread locals are not set massindexing fails when entities are loaded because the thread locals are not accessible by massindexer threads.
It would be great to have some extension point on massindexer thread creation to set thread locals.
would it be good enough if we were to allow to plug in some "hooks" to run at begin and end of each thread we use?
And we'd allow to set it on the MassIndexer:
that would be perfect. Like ThreadPoolExecutor has methods beforeExecute and afterExecute.
Ok we could add that. beforeExecute and afterExecute seem good names too.
Just to clarify, the plugged hook will be invoked by each thread, concurrently. So the implementor of the above interface will need to be threadsafe, but also you will need to consider that whatever you inject in the ThreadLocal, will also be used concurrently. Are you good with that?
Possibly you might need to use your beforeExecute hook to create a different (independent) copy of the services which you're injecting in the TheadLocal.
Out of curiosity, what are you having in these threadlocal variables?
That is perfect.
I will try to explain our use case:
For very special fields (legacy fields) but widely used we have implemented a hibernate CompositeUserType. This UserType maps the 1 side of some legacy n:1 entities. For the 1 side entities we implemented a proxy for custom lazy loading (legacy). So just like hibernate lazy loading an entity we can lazy load our legacy entity. This proxy on loading entities requires access to a thread local Accessor instance which is usually created and set by a servlet filter on every http request thread. The Accessor takes care of performance optimizations on loading legacy entities during the whole http request until the request/thread is finished (legacy caching). And therefore the same instance of the thread local should be bound to the thread and used throughout the whole request. In case of multiple threads we usually create new instances of this Accessor for each request and bind it as a thread local (like we do in asynchronuous jobs).