java - 使用 jasync-sql 执行查询时出错:“无法运行查询,因为已经有一个查询待处理”
问题描述
我们目前在 Ktor 服务中使用jasync-sql PostgreSQL 异步库,我们得到:
14:02:20.005 [DefaultDispatcher-worker-1] ERROR com.github.jasync.sql.db.postgresql.PostgreSQLConnection - Can't run query because there is one query pending already
14:02:20.008 [nettyCallPool-4-1] ERROR Application - Unhandled: GET - /api/customer_book_list
com.github.jasync.sql.db.exceptions.ConnectionStillRunningQueryException: <1> - There is a query still being run here - race -> false
at com.github.jasync.sql.db.postgresql.PostgreSQLConnection.notReadyForQueryError(PostgreSQLConnection.kt:297)
at com.github.jasync.sql.db.postgresql.PostgreSQLConnection.validateIfItIsReadyForQuery(PostgreSQLConnection.kt:305)
at com.github.jasync.sql.db.postgresql.PostgreSQLConnection.validateQuery(PostgreSQLConnection.kt:312)
at com.github.jasync.sql.db.postgresql.PostgreSQLConnection.sendPreparedStatement(PostgreSQLConnection.kt:133)
at util.PostgresClient$sendPreparedStatement$2.doResume(PostgresClient.kt:19)
at kotlin.coroutines.experimental.jvm.internal.CoroutineImpl.resume(CoroutineImpl.kt:42)
at kotlinx.coroutines.experimental.DispatchedTask$DefaultImpls.run(Dispatched.kt:168)
at kotlinx.coroutines.experimental.DispatchedContinuation.run(Dispatched.kt:13)
at kotlinx.coroutines.experimental.scheduling.Task.run(Tasks.kt:94)
at kotlinx.coroutines.experimental.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:567)
at kotlinx.coroutines.experimental.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
at kotlinx.coroutines.experimental.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:713)
我们是否必须为每个请求打开一个连接?或者可能是配置错误?
解决方案
在 jasync-sql 中,每个都Connection
可以一次处理一个语句执行。对于多个连接,最好的方法是使用ConnectionPool
这样的:
PoolConfiguration poolConfiguration = new PoolConfiguration(
100, // maxObjects
TimeUnit.MINUTES.toMillis(15), // maxIdle
10_000, // maxQueueSize
TimeUnit.SECONDS.toMillis(30) // validationInterval
);
Connection connectionPool = new ConnectionPool<>(
new PostgreSQLConnectionFactory (configuration), poolConfiguration);
Connection
本身没有用于挂起请求的缓冲区或队列,因此除非您想为每个请求创建一个低效的连接,否则应使用连接池。
可以在此处找到完整的 Kotlin 示例:https ://github.com/jasync-sql/jasync-sql/blob/master/samples/ktor/src/application.kt
推荐阅读
- r - 如何在 R 中使用 glm 循环多次曝光和结果?
- reactjs - 当您使用 multipart/form-data 内容类型拥有 javascript 对象时如何发布数据
- python - groupby 导致空数据框
- node.js - Amazon Rekogntion Image:错误 InvalidImageFormatException:请求的图像格式无效
- javascript - 将表单的复选框标签附加到
元素
- vue.js - Vue3 - 如何获取版本
- matlab - 是否可以从未经校准的校正图像对确定两个相机之间的基线?
- apache - Joomla:从 example.com/index.php/foo/bar 永久重定向到 example.com/foo/bar(URL 中没有 index.php)
- algorithm - 算法效率的大符号
- python - 循环遍历 IP 地址范围以检查 HTML 中的打印机状态 - Python & BeuatifulSoup