Merge branch '2.3.x'

Closes gh-23735
pull/23762/head
Stephane Nicoll 4 years ago
commit 9efff2af83

@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -26,7 +26,7 @@ import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.jpa.HibernateMetrics;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@ -50,11 +50,25 @@ import org.springframework.util.StringUtils;
SimpleMetricsExportAutoConfiguration.class })
@ConditionalOnClass({ EntityManagerFactory.class, SessionFactory.class, MeterRegistry.class })
@ConditionalOnBean({ EntityManagerFactory.class, MeterRegistry.class })
public class HibernateMetricsAutoConfiguration {
public class HibernateMetricsAutoConfiguration implements SmartInitializingSingleton {
private static final String ENTITY_MANAGER_FACTORY_SUFFIX = "entityManagerFactory";
@Autowired
private final Map<String, EntityManagerFactory> entityManagerFactories;
private final MeterRegistry meterRegistry;
public HibernateMetricsAutoConfiguration(Map<String, EntityManagerFactory> entityManagerFactories,
MeterRegistry meterRegistry) {
this.entityManagerFactories = entityManagerFactories;
this.meterRegistry = meterRegistry;
}
@Override
public void afterSingletonsInstantiated() {
bindEntityManagerFactoriesToRegistry(this.entityManagerFactories, this.meterRegistry);
}
public void bindEntityManagerFactoriesToRegistry(Map<String, EntityManagerFactory> entityManagerFactories,
MeterRegistry registry) {
entityManagerFactories.forEach((name, factory) -> bindEntityManagerFactoryToRegistry(name, factory, registry));

@ -35,6 +35,7 @@ import org.mockito.ArgumentMatchers;
import org.springframework.boot.actuate.autoconfigure.metrics.test.MetricsRun;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryBuilderCustomizer;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.boot.test.context.FilteredClassLoader;
@ -42,6 +43,8 @@ import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
@ -127,6 +130,21 @@ class HibernateMetricsAutoConfigurationTests {
});
}
@Test
void entityManagerFactoryInstrumentationDoesNotDeadlockWithDeferredInitialization() {
this.contextRunner
.withPropertyValues("spring.jpa.properties.hibernate.generate_statistics:true",
"spring.datasource.schema=city-schema.sql", "spring.datasource.data=city-data.sql")
.withBean(EntityManagerFactoryBuilderCustomizer.class,
() -> (builder) -> builder.setBootstrapExecutor(new SimpleAsyncTaskExecutor()))
.run((context) -> {
JdbcTemplate jdbcTemplate = new JdbcTemplate(context.getBean(DataSource.class));
assertThat(jdbcTemplate.queryForObject("SELECT COUNT(*) from CITY", Integer.class)).isEqualTo(1);
MeterRegistry registry = context.getBean(MeterRegistry.class);
registry.get("hibernate.statements").tags("entityManagerFactory", "entityManagerFactory").meter();
});
}
@Configuration(proxyBeanMethods = false)
static class BaseConfiguration {

@ -0,0 +1 @@
INSERT INTO CITY (ID, NAME, STATE, COUNTRY, MAP) values (2000, 'Washington', 'DC', 'US', 'Google');

@ -0,0 +1,7 @@
CREATE TABLE CITY (
id INTEGER IDENTITY PRIMARY KEY,
name VARCHAR(30),
state VARCHAR(30),
country VARCHAR(30),
map VARCHAR(30)
);

@ -8,6 +8,7 @@ description = "Spring Boot Data JPA smoke test"
dependencies {
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-data-jpa"))
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-web"))
implementation(project(":spring-boot-project:spring-boot-starters:spring-boot-starter-actuator"))
runtimeOnly("com.h2database:h2")

@ -1,4 +1,4 @@
spring.h2.console.enabled=true
spring.jpa.open-in-view=true
spring.data.jpa.repositories.bootstrap-mode=default
spring.data.jpa.repositories.bootstrap-mode=deferred
logging.level.org.hibernate.SQL=debug

Loading…
Cancel
Save