Reputation: 3496
In Spring based application, Transaction Manager is responsible for committing or rolling back SQL transactions. My application uses a custom cache for some part of persistent data. This cache is not managed by Spring nor Hibernate.
If a SQL transaction encounters errors and must be rolled back, then cache modifications should be rolled back as well.
My question is, how to register an event listener or callback which will call my cache.evict()
method when Spring Transaction Manager rolls back a transaction?
Upvotes: 1
Views: 836
Reputation: 2146
There are lots of ways of doing this...
Upvotes: 0
Reputation: 3943
You can use TransactionSynchronizationAdapter for this. For reference, you can look at this: Spring - commit JMS Transaction after JPA transaction
In the answer given in the link, the synchronization is registered for afterCommit
. In your case, it would be afterCompletion
. In that implementation, you will need to check if the transaction is in a rolled back state and do the cache eviction.
Upvotes: 2