Emmanuel Bernard: I've experimented with the owner idea and come to think that this won't work out as I hoped.
The problem is that the OgmSession would have to be built before the delegate is instantiated (which expects the owner to be passed to its constructor). This could be achieved by moving the construction of the delegate into the constructor of OgmSession but then I'm hitting a dead end. The super-type constructor SessionDelegatorBaseImpl expects the delegate to be passed to two parameters, which we only could achieve by building the delegate twice (or some very ugly tricks using static methods).
Given you mentioned OgmSession is designed stateless atm. (and there is no 1:1 relationship between OgmSession and the wrapped delegate) I contemplated the idea of passing a specific context object as owner to the delegate session. This should work, but apparently that would be quite an ab-use of the owner contract.
Another idea would be to continue with the thread local approach, but use a WeakIdentityHashMap, keyed by SessionImpl. This would make sure the context is kept per session and shouldn't cause session leaks once there are no other references.
|