【问题标题】:How to access datasource information in spring boot with spring data jpa and hibernate如何使用spring data jpa和hibernate在spring boot中访问数据源信息
【发布时间】:2021-02-04 13:22:12
【问题描述】:

我需要对我的应用程序执行运行状况检查,该应用程序使用带有 spring data jpa 和 hibernate 的 spring boot。 我需要用 jpa 来做到这一点,而不是针对任何实现。 在 EclipseLink 中,我们可以使用 EntityManagerFactoryDe​​legate,如下所示,但我不知道如何使用 spring data jpa 和 hibernate 来执行此操作。

EntityManagerFactoryDelegate  delegate = entityManager.getEntityManagerFactory().unwrap(EntityManagerFactoryDelegate.class);

PersistenceInfo = delegate.getSetupImpl().getPersistenceUnitInfo();
DataSourceImpl dataSource = (DataSource) info.getNpnJtaDataSource();
return dataSource.getName();

谁能建议我如何在 spring data jpa 中使用 hibernate 做到这一点。

【问题讨论】:

    标签: spring spring-boot hibernate spring-data-jpa


    【解决方案1】:

    您可以使用spring boot actuator 依赖进行健康检查,无需外部配置。一旦定义了数据源 Bean,它将自动选择数据库健康检查。

    如果您想启用/禁用数据库健康检查,您可以使用以下属性,

    management.health.db.enabled=<boolean, true || false>
    

    实现参考:https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html

    【讨论】:

      猜你喜欢
      • 2020-03-16
      • 2017-07-09
      • 1970-01-01
      • 2019-07-20
      • 2014-12-06
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多