Should testOnborrow of database connection pool be turned on or not in a production environment?

enabling affects performance, but does not enable it. If an abnormal connection occurs, it may not be reclaimed in time, causing the system thread to be occupied and then hung up. For example, https://blog.csdn.net/lx34832.

Mar.28,2021

Hello, it is generally not open in the production environment.

In the case of

TestOnBorrow=false, because the availability of connections in the pool is not tested, if the connections in the connection pool are closed by the database, the application may obtain these unavailable connections through the connection pool getConnection, and if these connections are not reclaimed by other threads, they will not be abolished or recreated, occupying the quota of the connection pool.

when TestOnBorrow=true, there are two situations:

1. When an instance in the cluster goes down, if the connection is not in the communication phase, the tcp connection is in the CLOSE_WAIT state or has been closed. When the application passes through the connection pool getConnection, it will detect the connection in the borrow. Because the connection has been closed, the following error is reported and a new connection is established, and the new connection is connected to other instances of the cluster. We can communicate normally in the back.

2. When an instance in the cluster goes down, if the connection happens to be in the communication stage, because the client cannot immediately perceive that the server has been disconnected, it may report the following error and wait for the response timeout from the server to report an error. When the application passes through the connection pool getConnection, the connection is detected during the borrow. Since the connection has been closed, the following error is reported and a new connection is re-established. At this time, the new connection is connected to other instances of the cluster. We can communicate normally in the back.

< hr >
  1. testOnBorrow and testOnReturn are generally not enabled in production environments, mainly because of performance considerations. Failed connections are mainly guaranteed by testWhileIdle. If unavailable database connections are obtained, exceptions are generally handled by the application.
  2. for general database connection pooling, the meaning and best practices of configuration parameters such as testOnBorrow can be found in the official documentation.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b34ff4-40f9c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b34ff4-40f9c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?