spring-boot - 如何在 Spring Boot 中配置连接池
问题描述
如何将我的 Spring Boot 服务配置为与 Postgres 数据库建立最多 2 个开放连接?应用程序仅由少数人在 PRODUCTION 上使用,我不想
我的pom:
(...)
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hibernate5</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1200-jdbc41</version>
<scope>provided</scope>
<exclusions>
<exclusion>
<artifactId>slf4j-simple</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.51</version>
</dependency>
<dependency>
<groupId>com.vladmihalcea</groupId>
<artifactId>hibernate-types-52</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
(...)
</build>
</project>
和我的 application.properties 文件:
spring.datasource.url=jdbc:postgresql://xxx
spring.datasource.username=xxx
spring.datasource.password=xxx
spring.datasource.maximum-pool-size=2 //I think it is not sufficient
info.app.name:xxx
我认为 spring.datasource.maximum-pool-size=2 是不够的。我应该在属性文件中添加什么?我应该从我的 pom 文件中排除 HikariCP 吗?
解决方案
从 Spring Boot 2.0 开始,Hikari是默认DataSource
实现。因此,如果你想在 Spring Boot 中配置( Hikeri ConfigurationmaximumPoolSize
的一个参数),你应该这样设置它:
对于 application.properties
spring.datasource.hikari.maximum-pool-size=2
对于应用程序.yml
spring:
datasource:
hikari:
maximum-pool-size: 2
推荐阅读
- java - 将数据写入Java中的外部文件
- c# - 是否可以在 C# 中使用两个类来引用外键?
- kubernetes - Kubernetes 中的 Pod 负载分布
- java - 使用带有 Java 枚举的 lombok @Getter 注解
- javascript - 使用 Filereader API 时如何设置下载的文件名?
- twitter-bootstrap - Bootstrap 拉伸链接在 Chrome 和 Edge 上断开,但在 Firefox 上有效
- swift - 如何让 RealityKit 只显示 CollisionComponents?
- rest - ReST - PUT vs PATCH 以在添加新属性时最大限度地减少客户端和 API 之间的耦合
- azure - 有没有办法阻止 azure app 服务的插槽交换
- html - 如何判断哪个 html 元素在服务器端发送了 http 请求?