0

Trying an interceptor for logging purposes. Created a repository that extends CrudRepository with Redis Entity. I have simple repository.save(entity) method.

My interceptor looks like below:

public class MyRepoInterceptor extends EmptyInterceptor {

  @Override
  public boolean onSave(Object entity, Serializable id, 
                      Object[] state, String[] propertyNames, Type[] types) {
     log.info("Saving...");
     return super.onSave(entity, id, state, propertyNames, types);
  }

  @Override
  public boolean onPrepareStatement(String sql)  {
     log.info("Prepared Statement");
     return super.onPrepareStatement(sql);
  }
}

I do not see the message Saving..., obviously interceptor is not working here.

However, in the same app, I have another repository for mySQL. In this, I have a repository method findByXXX. The interceptor is intercepting this method and I see the message Prepared Statement.

My interceptor Config class:

 @Component
 public class RepoInterceptor implements HibernatePropertiesCustomizer {

    @Autowired
    MyRepoInterceptor myRepoInterceptor;

    @Override
     public void customize(Map<String, Object> hibernateProperties) {
      //Tried this too. but no luck
      //hibernateProperties.put("hibernate.session_factory.interceptor", myRepoInterceptor); 
      hibernateProperties.put("hibernate.ejb.interceptor", myRepoInterceptor);
    }
 }

Has anyone tried this? All that I want is, log any calls to database from "Crud/JPARepository" methods anywhere in the app without forcing the developer to do the logging.

If above is not the right solution, any other better solution(s)?

Kevin Rave
  • 13,876
  • 35
  • 109
  • 173
  • Try adding spring.jpa.properties.hibernate.ejb.interceptor=.com.repository.MyRepoInterceptor In application.properties. – Satish Varma Dec 06 '20 at 12:22
  • Did that as well. No luck :-( – Kevin Rave Dec 06 '20 at 15:43
  • 1
    I'm confused. You're trying to make a Hibernate interceptor run for a Redis entity? How is that supposed to work? Spring Data does not use Hibernate for Redis, obviously. If you want to capture specific methods in repositories regardless of type, try [creating an aspect](https://stackoverflow.com/questions/34332046/aspect-advice-for-spring-data-repository-doesnt-work) targeting the base `CrudRepository` interface – crizzis Dec 06 '20 at 17:31
  • @crizzis You got me thinking! :-) I thought Redis as well uses Hibernate for operations. May be not? As for the Apsects, it does not have the information about the entity being operated on. So it won't work – Kevin Rave Dec 06 '20 at 17:52
  • Well, you can gain access to the invocation parameters inside an aspect, so you can get all the information from there. Not sure what your requirements are, though – crizzis Dec 06 '20 at 17:56
  • What does it mean by ` gain access to the invocation parameters inside an aspect, so you can get all the information from there.`. Can you throw me an example / link? – Kevin Rave Dec 06 '20 at 18:28

0 Answers0