Я использую vert.x SQLConnection для подключения к базе данных. где я выполняю простой запрос в цикле, используя то же соединение. Ниже приведен пример кода
public static void test(RoutingContext routingContext) {
Vertx vertx = Vertx.currentContext().owner();
JsonObject configJson = new JsonObject().put("host", "mymysqldb.mycompany");
io.vertx.ext.asyncsql.AsyncSQLClient client = PostgreSQLClient.createShared(vertx, configJson);
client.getConnection(connectionHandler -> {
if (connectionHandler.succeeded()) {
SQLConnection connection = connectionHandler.result();
for (int i = 0; i < 10; i++) {
System.out.println("count" + i);
try {
connection.query("select 1", selectHandler -> {
System.out.println("executed query");
});
} catch (Exception e) {
System.out.println(e);
}
}
} else {
LOGGER.error("Error in get connection : ", connectionHandler.cause());
}
});
}
но когда я запускаю это, я получаю следующий вывод
count0
count1
com.github.mauricio.async.db.exceptions.ConnectionStillRunningQueryException: [2] - There is a query still being run here - race -> false
count2
com.github.mauricio.async.db.exceptions.ConnectionStillRunningQueryException: [2] - There is a query still being run here - race -> false
count3
com.github.mauricio.async.db.exceptions.ConnectionStillRunningQueryException: [2] - There is a query still being run here - race -> false
count4
com.github.mauricio.async.db.exceptions.ConnectionStillRunningQueryException: [2] - There is a query still being run here - race -> false
count5
com.github.mauricio.async.db.exceptions.ConnectionStillRunningQueryException: [2] - There is a query still being run here - race -> false
count6
com.github.mauricio.async.db.exceptions.ConnectionStillRunningQueryException: [2] - There is a query still being run here - race -> false
count7
com.github.mauricio.async.db.exceptions.ConnectionStillRunningQueryException: [2] - There is a query still being run here - race -> false
count8
count9
com.github.mauricio.async.db.exceptions.ConnectionStillRunningQueryException: [2] - There is a query still being run here - race -> false
executed query
executed query
Я полагаю, это может быть из-за того, что он выполняет запрос асинхронно в цикле for и поскольку одно и то же соединение используется снова и снова, он пытается выполнить второй запрос с тем же соединением, пока первое еще работает.
Что ж, эта проблема будет решена, если я вызову метод getDatabaseConnection в самом цикле for, чтобы каждый запрос мог использовать разные соединения. (опять же, это может быть проблемой, если цикл равен тысяче, тогда будет создано много соединений, но давайте пока проигнорируем эту проблему). Но что, если я хочу использовать транзакцию здесь, тогда мне нужно создать одно соединение для транзакции?
Есть ли проблема с vert.x или в моем понимании. Может ли кто-нибудь помочь мне, как я могу достичь этого