Skip to content

HibernateMetricsAutoConfiguration will fail, when using different JPA implementation #15317

@ihrigb

Description

@ihrigb

When using a different JPA implementation than Hibernate, HibernateMetricsAutoConfiguration (link) will fail, as it is based on Hibernate components.

The following happens usually:
Hibernate is initialized before this auto configuration class. Then HibernateMetricsAutoConfiguration will be checked for conditions

@ConditionalOnClass({ EntityManagerFactory.class, MeterRegistry.class })
@ConditionalOnBean({ EntityManagerFactory.class, MeterRegistry.class })

The conditions are matching, so the auto configuration class will be taken into account.

Without Hibernate:
Any other JPA vendor (e. g. Eclipse Link) will be set up. Now, with micrometer also active, we also will have as well class and bean of EntityManagerFactory and MeterRegistry. Now again, HibernateMetricsAutoConfiguration will be taken into account. As now, hibernate is not in the classpath, we will get java.lang.ClassNotFoundException: org.hibernate.SessionFactory.

Suggestion for a fix
This should be easy to fix, by adding a third class to @ConditionalOnClass. That class should be specific to Hibernate, to make sure, that the JPA vendor really is Hibernate. So the fix could be:

@ConditionalOnClass({ EntityManagerFactory.class, MeterRegistry.class, SessionFactory.class })
@ConditionalOnBean({ EntityManagerFactory.class, MeterRegistry.class })

I can take care of this and provide a PR, if this is confirmed. Not sure, if there is a more appropriate way to tell, if the JPA vendor is Hibernate.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions