java - 强制 Hikari/Hibernate 关闭陈旧(泄漏?)的连接

标签 java spring hibernate filemaker hikaricp

我正在通过 the official JDBC driver 使用 FileMaker 16 数据源在带有 Hibernate 5.3 和 Hikari 2.7 的 Spring Boot 2 中。

FileMaker服务器性能较差,大表的SQL查询执行时间可达一分钟。有时,当连接池充满了从未释放的 Activity 连接时,会导致连接泄漏。

问题是如何强制池中挂起两分钟的 Activity 连接关闭,将它们移至空闲状态并可供再次使用。

作为示例,我使用 org.springframework.data.repository.PagingAndSortingRepository 中的 findAll 方法通过 RestController 访问 FileMaker 数据源:

@RestController
public class PatientController {

    @Autowired
    private PatientRepository repository;

    @GetMapping("/patients")
    public Page<Patient> find(Pageable pageable) {
        return repository.findAll(pageable);
    }
}

在原始情况下多次调用 /患者 会导致连接泄漏,Hikari 报告如下:

2018-09-20 13:49:00.939 DEBUG 1 --- [l-1 housekeeper] com.zaxxer.hikari.pool.HikariPool : HikariPool-1 - Pool stats (total=10, active=10, idle=0, waiting=2)

它还会抛出这样的异常:

java.lang.Exception: Apparent connection leak detected
at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:128) ~[HikariCP-2.7.9.jar!/:na]

我需要的是,如果repository.findAll花费超过N秒,则必须终止连接并且 Controller 方法必须抛出异常。如何实现?

这是我的 Hikari 配置:

 allowPoolSuspension.............false
 autoCommit......................true
 catalog.........................none
 connectionInitSql...............none
 connectionTestQuery............."SELECT COUNT(*) FROM Clinics"
 connectionTimeout...............30000
 dataSource......................none
 dataSourceClassName.............none
 dataSourceJNDI..................none
 dataSourceProperties............{password=<masked>}
 driverClassName................."com.filemaker.jdbc.Driver"
 healthCheckProperties...........{}
 healthCheckRegistry.............none
 idleTimeout.....................600000
 initializationFailFast..........true
 initializationFailTimeout.......1
 isolateInternalQueries..........false
 jdbc4ConnectionTest.............false
 jdbcUrl.........................jdbc:filemaker://***:2399/ec_data
 leakDetectionThreshold..........90000
 maxLifetime.....................1800000
 maximumPoolSize.................10
 metricRegistry..................none
 metricsTrackerFactory...........none
 minimumIdle.....................10
 password........................<masked>
 poolName........................"HikariPool-1"
 readOnly........................false
 registerMbeans..................false
 scheduledExecutor...............none
 scheduledExecutorService........internal
 schema..........................none
 threadFactory...................internal
 transactionIsolation............default
 username........................"CHC"
 validationTimeout...............5000

最佳答案

HikariCP 仅专注于连接池管理,以管理由此形成的连接。

loginTimeout - HikariCP 等待与数据库建立连接(基本上是 JDBC 连接)的时间

spring.datasource.hikari.connectionTimeout=30000

maxLifetime - 连接在关闭之前在池中存活的时间

spring.datasource.hikari.maxLifetime=1800000

idleTimeout - 未使用的连接在池中存活的时间

spring.datasource.hikari.idleTimeout=30000

如果请求时间超过定义的超时时间,请使用 javax.persistence.query.timeout 取消请求。

javax.persistence.query.timeout(长 - 毫秒)

The javax.persistence.query.timeout hint defines how long a query is allowed to run before it gets canceled. Hibernate doesn’t handle this timeout itself but provides it to the JDBC driver via the JDBC Statement.setTimeout method.

关于java - 强制 Hikari/Hibernate 关闭陈旧(泄漏?)的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52425743/

相关文章:

java - SWIG CYGWIN DLL 链接

java - 线程 "main"org.hibernate.AnnotationException : @org. hibernate.annotations.Table 中的异常引用未知表:ProductForPractice

java - Spring 事务传播和乐观锁定的问题

java - Hibernate自引用实体查询时重复字段

java - 当我使用组合时,我应该使用 "duplicate"字段吗?

Java Stream > 是否可以将 "orElseGet"内联到父流中?

java:停止单例线程

java - Spring MVC : No mapping found for HTTP request with URI [/hello. jsp]

java - RestController 与 GET + POST 相同的方法?

java - Spring MVC 数据绑定(bind) - 原始类型