【问题标题】:How does JPA entityManager choose Connection Pool in Spring Boot Application?Spring Boot Application中JPA entityManager如何选择连接池?
【发布时间】:2019-07-20 06:49:01
【问题描述】:

我有 2 个DAO - 第一个 与 @Bean DataSource 一起使用+ JDBC配置如下:

@Bean("dataSource")
    @Singleton
    public DataSource getDataSource() {
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName("...");
        basicDataSource.setUrl("...");
        basicDataSource.setUsername(...);
        basicDataSource.setPassword(...);
        ...
        return basicDataSource;
    }

secondentityManager 一起使用。 application.properties 配置如下:

spring.datasource.url=...
spring.datasource.username=...
spring.datasource.password=...
...

当我启动我的 Spring Boot 应用程序 并且 spring 初始化我的 beans 时,我使用 second DAO 来从数据库获取一些信息。

在这种情况下,我使用 second DAO -> entityManager

我希望 entityManager 使用 application.properties 中的 配置
实际上,entityManager 使用了来自 bean DataSourceconfigration

它是如何工作的?
p.s. application.properties 中的数据库属性看起来像是使用过的。

实际上我认为我应该为我的应用程序使用一个 ConnectionPool
我可以将 DataSource 配置为 @Bean 并提供 entityManagerjdbcTemplate 使用它。
我应该选择其他解决方案吗?还是这个想法很合适?

【问题讨论】:

  • A JPA EntityManager 仅使用定义了 EntityManagerFactoryDataSource(假设向其提供了数据源)。数据源可能会预先从 ConnectionPool 中定义
  • 但为什么 entityManager 从@Bean 中选择 dataSource,而不是从 application.properties 中选择?我想它从 application.properties 和bean之后获取它,否则我不知道为什么在 application.properties 数据库属性看起来像使用过。
  • EntityManager 来自 JPA 规范。 JPA 规范中没有application.properties。也许你应该阅读它。你所说的是SPRING,而不是JPA。 SPRING 定义了数据源以及它传递给 JPA 的内容

标签: java spring database jpa spring-data-jpa


【解决方案1】:

这是因为重要性。 @Configuration 的优先级高于 application.properties。首先 spring-boot 搜索 @Bean 定义,如果没有找到,则检查 application.properties。通常这些定义是等价的。

【讨论】:

    猜你喜欢
    • 2017-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-24
    • 1970-01-01
    • 2015-08-25
    • 1970-01-01
    相关资源
    最近更新 更多