I want to call a method (that calls a Postgres function) every time a transaction is started.
I'm not sure where to start, but I'm using JpaTransactionManager.
I want to call a method (that calls a Postgres function) every time a transaction is started.
I'm not sure where to start, but I'm using JpaTransactionManager.
The best approach is to use Aspect Oriented Programming (AOP). AOP will help you to write your method once and define a pointcut. What's AOP? you can read What's AOP?
My Approach to that would be:
Create a Class that replaces the JPATransactionManager by delegating all calls to a hidden local instance of JPATransactionManager. This class additionally can provide the possibility to register observers. When configuring your Spring-Container use this new class instead of JPATransactionManager, all begin Transaction-Calls should be routed through that.
Depending on the solution of the Transaction-Object, if you want to be sure, that you catch all begin calls, you can create a delegate to the transaction-object returned by the TransactionManager as well and return this delegate by your TransactionManager-Delegate.