brettwooldridge / HikariCP

光 HikariCP・A solid, high-performance, JDBC connection pool at last.
Apache License 2.0
19.85k stars 2.92k forks source link

How does hikariCP actively disconnect #2230

Open Caesar2816 opened 1 month ago

Caesar2816 commented 1 month ago

How does hikariCP set the SQL timeout period to ensure that the global SQL automatically disconnects the connection after 30 seconds of execution, so that the connection is not full because the connection is not released while holding the connection

The following configurations cannot be actively disconnected after testing, but will continue to perform: spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.validation-timeout=30000

IRus commented 4 weeks ago

@Caesar2816 you need to setup socket timeout property on jdbc driver properties, i.e:

spring.datasource.url=jdbc:mysql://localhost:3306/dbname?socketTimeout=30000
spring.datasource.url=jdbc:postgresql://localhost:5432/dbname?socketTimeout=30000
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=dbname;socketTimeout=30

Postgres

https://jdbc.postgresql.org/documentation/use/

socketTimeout (int) Default 0 The timeout value used for socket read operations. If reading from the server takes longer than this value, the connection is closed. This can be used as both a brute force global query timeout and a method of detecting network problems. The timeout is specified in seconds max(2147484) and a value of zero means that it is disabled.

MariaDB

https://mariadb.com/docs/server/connect/programming-languages/java/connect/

Defines the network socket timeout (SO_TIMEOUT) in milliseconds. When set to 0, there is no socket timeout.

JDBC

Or, you can set it directly on the prepared statement:

statement.setQueryTimeout(30); // Timeout in seconds
Caesar2816 commented 4 weeks ago

Sorry for forgetting to mark the ORACLE database I used clarify sprintboot 2.1.3.RELEASE HikariCP 2.7.9 ojdbc819.3.0.0 mybatis-spring-boot-starter 2.1.3

The following methods have now been tested The first: mybatis.configuration.default-statement-timeout=30 spring.datasource.hikari.connection-timeout=30000 spring.datasource.hikari.validation-timeout=30000 spring.jdbc.template.query-timeout=30 Adding the above three configurations does not work

The second kind: custom datasouce to set the timeout time is not good Third: rewrite sqlsession implementation class manual set timeout time is not good Fourth: The interface does not add @transaction() Fifth: Custom mybatis blocker does not work Sixth: Add @Options() annotation above the interface of the dao layer to add timeout duration is not good Seventh: xml file