8

After upgrade from Spring Boot 2.0.0 M2 to 2.0.0 M6 my Hibernate interceptor implementation don't work anymore.

My old implementation:

@Configuration
public class HibernateConfiguration extends HibernateJpaAutoConfiguration {

    private HibernateStatisticsInterceptor hibernateStatisticsInterceptor;

    public HibernateConfiguration(DataSource dataSource, JpaProperties jpaProperties, ObjectProvider<JtaTransactionManager> jtaTransactionManager, ObjectProvider<TransactionManagerCustomizers> transactionManagerCustomizers, ObjectProvider<List<SchemaManagementProvider>> providers, HibernateStatisticsInterceptor hibernateStatisticsInterceptor) {
        super(dataSource, jpaProperties, jtaTransactionManager, transactionManagerCustomizers, providers);
        this.hibernateStatisticsInterceptor = hibernateStatisticsInterceptor;
    }

    @Override
    protected void customizeVendorProperties(Map<String, Object> vendorProperties) {
        vendorProperties.put("hibernate.session_factory.interceptor", hibernateStatisticsInterceptor);
    }
}

But with M5 or M6 the HibernateJpaAutoConfiguration class changed and extends JpaBaseConfiguration no more.

I try to add my interceptor per YAML-Configuration file, but it's not working.

My Interceptor:

@Component("hibernateStatisticsInterceptor")
public class HibernateStatisticsInterceptor extends EmptyInterceptor {

    private static final long serialVersionUID = 5278832720227796822L;

    private ThreadLocal<Long> queryCount = new ThreadLocal<>();

    public void startCounter() {
        queryCount.set(0l);
    }

    public Long getQueryCount() {
        return queryCount.get();
    }

    public void clearCounter() {
        queryCount.remove();
    }

    @Override
    public String onPrepareStatement(String sql) {
        Long count = queryCount.get();
        if (count != null) {
            queryCount.set(count + 1);
        }
        return super.onPrepareStatement(sql);
    }

}

How can I reactivate my interceptor?

Thanks for any hints

regards Rizzi

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
RizziCR
  • 190
  • 2
  • 10
  • Can you try to extend from https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.java instead of HibernateJpaAutoConfiguration? – varren Nov 24 '17 at 07:17
  • This is not a public class and can't use outside it's package :/ – RizziCR Nov 24 '17 at 11:28
  • After some more research I find a way to add my Hibernate interceptor. You can add an interceptor by properties/yml file, but I try the wrong place first. It should be `spring.jpa.properties.hibernate.session_factory.interceptor = ` My new Problem is, that this hibernate creates his own instance of this class and is different to my instance from spring :/ regards Rizzi – RizziCR Nov 24 '17 at 21:51

3 Answers3

7

I can solve this with this documentation:

Add support for advanced customization of Hibernate settings

Just implements a interface HibernatePropertiesCustomizer

and implements methods customize(Map<String, Object> hibernateProperties)

@Component
class MyHibernateInterceptorCustomizer implements HibernatePropertiesCustomizer {

    @Autowired
    MyInterceptor myInterceptor

    @Override
    void customize(Map<String, Object> hibernateProperties) {
       hibernateProperties.put("hibernate.session_factory.interceptor", myInterceptor);
    }
}
lkamal
  • 3,788
  • 1
  • 20
  • 34
Rita Pissarra
  • 561
  • 5
  • 10
7

Hello,

Give this a read: https://github.com/spring-projects/spring-boot/commit/59d5ed58428d8cb6c6d9fb723d0e334fe3e7d9be (use: HibernatePropertiesCustomizer interface)

  1. Implement it.
  2. @Override customize() method, add your interceptor
  3. Don't forget to @Lazy inject in case of internal beans
  4. Done => Run

Hope this was of use to you.

As a final note: always update your Spring / Hibernate versions (use the latest as possible) and you will see that most code will become redundant as newer versions try to reduce the configurations as much as possible.

Rareș Flueraș
  • 324
  • 5
  • 4
  • The `@Lazy` is the trick to get it done while autowiring `repositories` to the Interceptor. Thank you, you just saved my time. – Vinh VO May 20 '19 at 15:25
4

As https://github.com/spring-projects/spring-boot/issues/11211 has been resolved, HibernatePropertiesCustomizer can be used since Spring Boot 2.0.0.RC1 (not released at the moment of writing yet but snapshots are available now).

Johnny Lim
  • 5,623
  • 8
  • 38
  • 53