-1

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.

Grant Foster
  • 722
  • 2
  • 11
  • 21
  • 2
    JPA is just a specification, you are using a particular implementation such as Hibernate. That implementation will have a way to add a listener so you can hook into the lifecycle of a transaction. – Gimby Oct 25 '18 at 09:15
  • 1
    See hear for an outline on Spring's support for hooking into the transaction lifecycle. https://stackoverflow.com/questions/23651464/spring-hibernate-how-to-call-some-method-after-transaction-commit-or-transacti/23653651#23653651 – Alan Hay Oct 25 '18 at 12:12

2 Answers2

1

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?

Tayyab Razaq
  • 348
  • 2
  • 11
1

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.

aschoerk
  • 3,333
  • 2
  • 15
  • 29