首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用jasync-sql执行查询时出错:“无法运行查询,因为已有一个查询挂起”

使用jasync-sql执行查询时出错:“无法运行查询,因为已有一个查询挂起”
EN

Stack Overflow用户
提问于 2018-10-20 04:32:33
回答 1查看 353关注 0票数 0

我们目前在Ktor服务中使用jasync-sql PostgreSQL异步库,我们得到了:

代码语言:javascript
复制
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)

我们必须为每个请求打开一个连接吗?或者可能是配置错误?

EN

回答 1

Stack Overflow用户

发布于 2018-10-20 04:32:33

在jasync-sql中,每个Connection一次可以处理一条语句的执行。对于多个连接,最好的方法是使用这样的ConnectionPool

代码语言:javascript
复制
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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52899531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档