postgresql - 带有 Hikari 的 Spring JPA 不释放连接

标签 postgresql hibernate spring-boot jpa hikaricp

我有一个带有 Controller 的 spring boot 应用程序。当我尝试同时点击一个 API(35 次并行点击)时,它会到达 Controller ,但我正在使用 crudrepository 的 findOne 来检查 postgres 的一些数据。 findOne 函数以毫秒为单位返回,但下一个 API 没有获得 postgres 连接。

我正在使用 HikariCp 作为具有以下设置的连接池

hikari:
 idleTimeout: 10000
 connectionTimeout: 60000
 maximumPoolSize: 30
 minimumIdle: 2
 poolName: gor-srms
 leakDetectionThreshold: 10000

简而言之,在任何时间点,只有 30 个 API 并行工作。 API 完成后,只有下一个获得 postgres 连接。

为什么 postgres 连接没有被其他 API 使用,因为 findOne 调用只需要几毫秒就可以完成?

添加完整的应用程序属性:

server:
   port: ${SERVER_PORT:${PORT:8093}}
   contextPath: ${mdm.service.contextPath:/wms-masterdata}

spring:
  application:
    name: ${mdm.service.name:mdm-service}
  jmx:
    enabled: false
  profiles:
    active: local
  cloud:
    config:
      discovery:
        enabled: false
        serviceId: config-server
  jpa:
      hibernate:
        ddl-auto: validate
  http:
      multipart:
        max-file-size: 20MB
        max-request-size: 20MB

discovery:
  enabled: true

eureka:
  client:
    enabled: ${discovery.enabled:true}
    serviceUrl:
      defaultZone: ${discovery.url:${REGISTRY_SERVICE_URL:http://localhost:8761}}/eureka/
  instance:
    metadataMap:
      contextPath: ${server.contextPath}

db:
  driver: org.postgresql.Driver
  url: jdbc:postgresql://${database.ip:localhost}:5432/wms_masterdata
  username: postgres
  password: postgres

hikari:
  idleTimeout: 10000
  connectionTimeout: 60000
  maximumPoolSize: 10
  minimumIdle: 2
  poolName: gor-mdm
  leakDetectionThreshold: 60000

hibernate:
  unit_name: wms_masterdata
  show_sql: false
  generate_ddl: false

entitymanager:
  packagesToScan: com.gor.platform.mdm.service.model

butler:
  url: https://192.168.8.116

management:
  security:
    enabled: false
  health:
    db:
      enabled: false

security:
  basic:
    enabled: false

flyway:
  baseline-on-migrate: true
  enabled: true
  ignore-ignored-migrations: true

最佳答案

spring.datasource.TYPE=com.zaxxer.hikari.HikariDataSource
spring.datasource.hikari.minimumIdle=5
spring.datasource.hikari.maximumPoolSize=20
spring.datasource.hikari.idleTimeout=30000
spring.datasource.hikari.poolName=SpringBootJPAHikariCP
spring.datasource.hikari.maxLifetime=2000000
spring.datasource.hikari.testWhileIdle=true
spring.datasource.hikari.validationQuery=SELECT 1
spring.datasource.hikari.connectionTimeout=30000 

采用这种设置有助于实现高效的连接池并允许服务器处理更多流量。

关于postgresql - 带有 Hikari 的 Spring JPA 不释放连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54779350/

相关文章:

java - Hibernate 更新子集合

mysql - 如何使事务在 Grails 中工作

java - 如何排除对象属性

java - 我们用mvc形式链接dao实体类好不好

sql - Django:在可以使用这种多对多关系之前,对象需要具有字段 "..."的值

sql - 需要查询帮助——聚合和多重连接

postgresql - 避免 PostgreSQL 中的嵌套循环

java - hibernate 搜索(Lucene + hibernate 核心)如何工作?

java - application.properties 与 applicationContext.xml

ruby - 将 Redis 事务包装在数据库事务中