0

I have a Spring Boot micro-service which has an endpoint to delete the entry from a table. I am using Spring Data JPA to perform this. It works fine when I call the api, HTTP request sent and JPA repository deletes the data. OSIV is enabled which automatically register an EntityManager, creates database connection and make sure it is available throughout the lifespan of web request.

I have a Redis message subscriber provided by Spring Redis in the same application which listen the event and calls the same JPA repository to delete the data from table. I noticed no delete/update/insert are being performed by JPA repository when called from subscriber. I understand there is an issue with transaction which is not being created in this flow. I tried with @Transactional but it is still failing as transaction is missing.

Please help me if Redis Message Container requires some other configuration to enable transaction for JPA repository.

Amit
  • 36
  • 7
  • OSIV is implemented as a SpringMVC Interceptor in this case. So it makes sense that Redis subscriber doesn't invoke it. But if EntityManager is not created by OSIV, it must be created by `@Transactional`. You need to check what's wrong with `@Transactional` - did you enable it in the first place? – Stanislav Bashkyrtsev May 07 '20 at 19:33
  • @StanislavBashkyrtsev: yes I enabled the @ Transactional (tried on service class and public method level too) but did not work. I injected the service class in my redis subscriber message class to make sure all conditions are met for proxy and trasanctional annotation to work but did not get any luck. – Amit May 08 '20 at 10:59
  • So if you stop at a breakpoint in your transactional method, do you see in the stack trace `TransactionAspectSupport`, `TransactionInterceptor`? – Stanislav Bashkyrtsev May 08 '20 at 11:12
  • @StanislavBashkyrtsev : yes it is there in stack trace. I am using deleteAll() method of CrudRepository which internally ideally calls findAll() and then make delete call for each entries from the table. But in this case this CrudRepository making findAll() call in logs but not making deleting call. However in call is from HTTP request, it makes both find and delete call withing findAll() call. – Amit May 08 '20 at 11:19
  • @StanislavBashkyrtsev : When call is from HTTP request, I can see in debug log it is using org.hibernate.engine.transaction.internal.TransactionImpl to do the DB operation (delete) but in case of subscriber with @ Transactional , it is not using any internal TransactionImpl – Amit May 08 '20 at 11:33
  • @StanislavBashkyrtsev . This has been resolved by creating a bean of JpaTransactionManager and marking it as primary – Amit May 11 '20 at 06:59
  • I don't think this is the right solution, seems like some random actions that led to more or less correct result. Unless you have distributed transactions you need to stick with JpaTransactionManager. – Stanislav Bashkyrtsev May 11 '20 at 08:46

0 Answers0