Ошибка при выполнении запроса с помощью jasync-sql: «Невозможно выполнить запрос, поскольку уже существует один ожидающий запрос» - PullRequest
0 голосов
/ 19 октября 2018

В настоящее время мы используем jasync-sql Асинхронная библиотека PostgreSQL в службе Ktor, и мы получаем:

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)

Нужно ли открывать соединение для каждого запроса?Или, может быть, конфигурация неверна?

1 Ответ

0 голосов
/ 19 октября 2018

В 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

...