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);
}
}
It's possible to add the interceptor as property "spring.jpa.properties.hibernate.session_factory.interceptor", but this is a hibernate instance and not accessible in the spring context.
It would be nice, if in future versions it's possible again, to use a spring controlled bean as hibernate interceptor, with auto configuration.
@RizziCR extending an auto-configuration is a smell, please don't do that. Rather, it would have been nice to report the feature here so that we implement it properly.
I'll give it some thoughts, thanks for reporting it!
This was prompted by a short discussion on Twitter. It feels similar to what we did in M7 for the naming strategies. It feels like a more general purpose callback for customising the Hibernate properties may be in order here. We could, perhaps, revert the bean-based naming strategy support and document the use of the callback instead.
I think the only change needed to support this use-case (and in general customization of vendor jpa properties for the EntityManagerFactory) is to make the org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration class public so that people can simply extend from it and override customizeVendorProperties() - just what @RizziCR did with the auto configuration class, only that now the functionality moved from the auto configuration class into the HibernateJpaConfiguration class.
@httpdigest I disagree. This class is internal and was made private for a very good reason.
Alright then. You of course have more insight into the matter. All I'm saying is that people have a use-case which is not supported by Spring Boot right now and I was proposing a _working_ workaround.
I am not following. This issue is going to provide a first-class support for what you're trying to do as far as I understood #11357. If that does not, feel free to comment there.
Works fine.. Thanks!
Works how? I can not solve this with Spring Boot 2.0.0.RELEASE...
The commit that closed this issue Includes an update to the documentation that describes how the new functionality works. If you need some help beyond that, please ask on Stack Overflow or Gitter.
Works fine.. Thanks!
How did you get it to work ? I'm still unable to use Spring Injection to instantiate my Hibernate Interceptor :(
Is the fix only available in Spring 2.1.1 ? As I'm using 2.0.3 but your commit changes is available, it seems the bugfix was merger on multiple versions tho
@Vespira if you have trouble using this feature, please ask on StackOverflow. There is a comment above yours with everything you need as far as Spring Boot is concerned. As indicated by the milestone, this issue is fixed as of 2.0.0.RC1.
@Vespira if you have trouble using this feature, please ask on StackOverflow. There is a comment about yours with everything you need as far as Spring Boot is concerned. As indicated by the milestone, this issue is fixed as of
2.0.0.RC1.
Ok merci St茅phane :) Not used to the procedure on Github, I posted a Stackoverflow topic.
Most helpful comment
This was prompted by a short discussion on Twitter. It feels similar to what we did in M7 for the naming strategies. It feels like a more general purpose callback for customising the Hibernate properties may be in order here. We could, perhaps, revert the bean-based naming strategy support and document the use of the callback instead.