如何在Spring Boot中配置连接池

时间:2019-10-23 21:31:06

标签: spring-boot connection-pooling

如何将我的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吗?

2 个答案:

答案 0 :(得分:1)

如果您尚未更改Spring使用的连接实现池,则应该为HikariCP,然后您要查找的设置为: spring.datasource.hikari.maximum-pool-size

它在这里列出:https://docs.spring.io/spring-boot/docs/current/reference/html/appendix-application-properties.html

答案 1 :(得分:1)

在Spring Boot 2中,Hikari是默认的DataSource实现。因此,如果要在Spring Boot中配置maximumPoolSizeHikeri Configuration的参数),则应按以下步骤进行设置:

对于application.properties

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

对于application.yml

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