Spring Boot中如何配置连接池

问题描述 投票:0回答:2

如何配置我的 Spring Boot 服务以最多有 2 个与 Postgres 数据库的打开连接?应用程序仅由少数人在生产中使用,我不想这样做

我的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.名称:xxx

我认为 spring.datasource.maximum-pool-size=2 是不够的。我应该在属性文件中添加什么?我应该从我的 pom 文件中排除 HikariCP 吗?

spring-boot connection-pooling
2个回答
3
投票

自 Spring Boot 2.0 起,Hikari 是默认的

DataSource
实现。因此,如果你想在Spring Boot中配置
maximumPoolSize
Hikeri Configuration的一个参数),你应该如下设置:

对于应用程序属性

spring.datasource.hikari.maximum-pool-size=2

申请.yml

spring:
  datasource:
    hikari:
      maximum-pool-size: 2

1
投票

如果你没有改变Spring正在使用的连接实现池,它应该是HikariCP,然后你要寻找的设置是: spring.datasource.hikari.最大池大小

此处列出:https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html

© www.soinside.com 2019 - 2024. All rights reserved.