1

这是连接池管理的文档,看起来maxIdleTime属性负责关闭连接。但不幸的是,每 4-5 秒 3 调试消息仍然带有一些奇怪的标题testAvailableItems

这是可重现的代码,下面是日志。

val dataTableName: String = "prefix_data",
val logTableName: String = "prefix_log",
private val suspendingConnection: SuspendingConnection =
        PostgreSQLConnectionBuilder
            .createConnectionPool(
                "postgres://$host:$port/$database?user=$username&password=$password"
            ) {
                maxIdleTime = TimeUnit.SECONDS.toMillis(10), // 10 seconds (in millis, Long value)
            }.asSuspending

suspend fun setup() = suspendingConnection
    .sendQuery(
        """
        |CREATE TABLE IF NOT EXISTS $dataTableName (
        |  id serial primary key,
        |  plugin_name varchar(45) NOT NULL,
        |  key varchar(90) NOT NULL,
        |  data text NOT NULL,
        |  timestamp timestamp DEFAULT current_timestamp,
        |  UNIQUE(plugin_name,key)
        |);
        |
        |CREATE TABLE IF NOT EXISTS $logTableName (
        |  id serial primary key,
        |  origin varchar(45) NOT NULL,
        |  message text NOT NULL,
        |  timestamp timestamp DEFAULT current_timestamp
        |);
        """.trimMargin()
    )

suspend fun main() {
    setup()
    // It does sends the close message after 10 seconds but still the logs increase rapidly every 5 seconds with a title `testAvailableItems`
}
[db-sql-netty-thread-1] TRACE com.github.jasync.sql.db.postgresql.codec.PostgreSQLConnectionHandler - got message com.github.jasync.sql.db.postgresql.messages.backend.ReadyForQueryMessage@780b88c7
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - received message: GiveBack: <postgres-connection-1> hasError=false ; availableItems=0 waitingQueue=0 inUseItems=1 inCreateItems=0 ActorCoroutine{Active}@1f6f7d35
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - add <postgres-connection-1> to available items, size is 1
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - scheduleNewItemsIfNeeded - availableItems=1 waitingQueue=0 inUseItems=0 inCreateItems=0 ActorCoroutine{Active}@1f6f7d35
[pool-1-thread-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - testAvailableItems - starting
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - received message: TestAvailableItems @1931541018 ; availableItems=1 waitingQueue=0 inUseItems=0 inCreateItems=0 ActorCoroutine{Active}@1f6f7d35
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - test: <postgres-connection-1> available 12687 ms
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - releasing idle item <postgres-connection-1>
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - destroy item <postgres-connection-1>
[db-sql-netty-thread-1] TRACE com.github.jasync.sql.db.postgresql.codec.MessageEncoder - Sending message CloseMessage
0: 58 00 00 00 04              X . . . . 
Total 5 bytes read

[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - testAvailableItems - done testing
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - scheduleNewItemsIfNeeded - availableItems=0 waitingQueue=0 inUseItems=0 inCreateItems=0 ActorCoroutine{Active}@1f6f7d35
[db-sql-netty-thread-1] INFO com.github.jasync.sql.db.postgresql.codec.PostgreSQLConnectionHandler - Connection disconnected - null
[pool-1-thread-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - testAvailableItems - starting
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - received message: TestAvailableItems @1771095482 ; availableItems=0 waitingQueue=0 inUseItems=0 inCreateItems=0 ActorCoroutine{Active}@1f6f7d35
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - testAvailableItems - done testing
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - scheduleNewItemsIfNeeded - availableItems=0 waitingQueue=0 inUseItems=0 inCreateItems=0 ActorCoroutine{Active}@1f6f7d35
[pool-1-thread-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - testAvailableItems - starting
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - received message: TestAvailableItems @1904368932 ; availableItems=0 waitingQueue=0 inUseItems=0 inCreateItems=0 ActorCoroutine{Active}@1f6f7d35
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - testAvailableItems - done testing
[DefaultDispatcher-worker-1] TRACE com.github.jasync.sql.db.pool.ActorBasedObjectPool - scheduleNewItemsIfNeeded - availableItems=0 waitingQueue=0 inUseItems=0 inCreateItems=0 ActorCoroutine{Active}@1f6f7d35
// ... these 3 lines continues forever every 5 seconds

当数据库不使用时(例如10秒不使用后),如何真正关闭连接?

4

1 回答 1

1

连接实际上是关闭的。
您在此处看到的日志消息是池本身每 5 秒触发一次实时连接测试。
您可以在消息中看到:(availableItems=0 waitingQueue=0 inUseItems=0 inCreateItems=0每个项目都是一个连接)这意味着没有打开的连接。

于 2020-06-19T11:23:47.737 回答