RizziCR
RizziCR

Reputation: 190

Spring Boot 2.0.0 M6 - Add Hibernate Interceptor

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

Upvotes: 8

Views: 5383

Answers (3)

Rita Pissarra
Rita Pissarra

Reputation: 581

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);
    }
}

Upvotes: 7

Rareș Flueraș
Rareș Flueraș

Reputation: 324

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.

Upvotes: 7

Johnny Lim
Johnny Lim

Reputation: 5843

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).

Upvotes: 4

Related Questions