Announcement Announcement Module
No announcement yet.
Custom cache synchronization - Spring/Hibernate integration Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Custom cache synchronization - Spring/Hibernate integration

    I want to implement a transaction synchronization that will cluster-wide invalidate caches for transactional data.

    Looks like a use case for TransactionSynchronizationManager. However, this distributed invalidation also needs to take care of Hibernate second level caching (in this case JVM scoped with cluster-wide invalidation to be added). The integration requirements would be:

    A synchronization listener that can fire cache invalidations from afterCompletion. This listener would need to be called after any Hibernate related afterConmpletion handling is done.

    The listener must be installed when the hibernate session is bound to the transaction (or latest when the first object modification occurs in the session) and prior to transaction completion. (Hibernate session may not be flushed prior to beforeCompletion and cache updates will occur as part of session flush.)

    Here is the issue with using TransactionSynchronizationManager:

    (1) It is too late to install the custom synchronization when the cache updates occur, because the TM is already iterating the list of listeners and a concurrent access exception will be thrown.

    (2) There does not seem to be a way to intercept the session creation, final behavior of org.springframework.orm.hibernate.SessionFactoryUt ils. Hence there is also no way to make sure the custom listener gets installed after the SessionFactoryUtils listener. This is needed as even afterCompletion may cause cache invalidations and those need to be batched and send out as the final step.

    To work around I was trying to use a Hibernate session interceptor and ensure in each and every callback that the tx listener gets installed if not present already. This approach is confusing and certainly not beneficial for performance.

    The only other option I see is to customize the transaction manager itself (change the behavior of commit), something I would have preferred to avoid to keep the TM implementation pluggable (JTA, DataSource, Hibernate) for units testing purposes etc.

    Is there any other (hidden?) feature in Spring that I would be able to use? Transaction demarcation is declarative in this setup, is there something less intrusive that could be done at the proxy level?

    Any insight and suggestions appreciated,