Vert.x中的SQLConnection和SqlConnection什么时候关闭(释放)比较好?
When is good to close (release) the SQLConnection and SqlConnection in Vert.x?
取自 Vert.x 网站示例:
private Future<Void> prepareDatabase() {
Promise<Void> promise = Promise.promise();
dbClient = JDBCClient.createShared(vertx, new JsonObject() //(1)
.put("url", "jdbc:hsqldb:file:db/wiki") //(2)
.put("driver_class", "org.hsqldb.jdbcDriver") //(3)
.put("max_pool_size", 30)); //(4)
dbClient.getConnection(ar -> { //(5)
if (ar.failed()) {
LOGGER.error("Could not open a database connection", ar.cause());
promise.fail(ar.cause()); //(6)
} else {
SQLConnection connection = ar.result(); //(7)
connection.execute(SQL_CREATE_PAGES_TABLE, create -> {
connection.close(); //(8)
if (create.failed()) {
LOGGER.error("Database preparation error", create.cause());
promise.fail(create.cause());
} else {
promise.complete(); //(9)
}
});
}
});
return promise.future();
}
在 (8) 中,连接在处理程序的最开始关闭。如果我们执行查询然后在处理程序中迭代结果会怎样:
private fun jdbcQuery(sql: String, params: JsonArray): Future<ResultSet> {
val promise: Promise<ResultSet> = Promise.promise()
getJDBCClient().getConnection { ar ->
if (ar.succeeded()) {
val connection = ar.result()
connection.queryWithParams(sql, params) { res ->
connection.close() //(10) release the connection
if (res.succeeded()) {
val result = res.result()
promise.complete(result)
} else {
promise.fail(res.cause())
}
}
} else {
promise.fail(ar.cause())
}
}
return promise.future()
}
我可以获取里面的数据if (res.succeeded())
。
我的问题是:为什么我们可以在迭代获取数据之前关闭并释放连接?它是如何工作的?
queryWithParams
API 在执行时从数据库中获取整个响应。不会延迟获取结果。因此,在响应处理程序回调开始时关闭连接是安全的,因为到那时客户端已经接收到整个结果集。只有当您使用 queryStream
API 时才会延迟获取结果。如果您正在使用 API,您可能希望等到所有结果都收到后才关闭连接。
取自 Vert.x 网站示例:
private Future<Void> prepareDatabase() {
Promise<Void> promise = Promise.promise();
dbClient = JDBCClient.createShared(vertx, new JsonObject() //(1)
.put("url", "jdbc:hsqldb:file:db/wiki") //(2)
.put("driver_class", "org.hsqldb.jdbcDriver") //(3)
.put("max_pool_size", 30)); //(4)
dbClient.getConnection(ar -> { //(5)
if (ar.failed()) {
LOGGER.error("Could not open a database connection", ar.cause());
promise.fail(ar.cause()); //(6)
} else {
SQLConnection connection = ar.result(); //(7)
connection.execute(SQL_CREATE_PAGES_TABLE, create -> {
connection.close(); //(8)
if (create.failed()) {
LOGGER.error("Database preparation error", create.cause());
promise.fail(create.cause());
} else {
promise.complete(); //(9)
}
});
}
});
return promise.future();
}
在 (8) 中,连接在处理程序的最开始关闭。如果我们执行查询然后在处理程序中迭代结果会怎样:
private fun jdbcQuery(sql: String, params: JsonArray): Future<ResultSet> {
val promise: Promise<ResultSet> = Promise.promise()
getJDBCClient().getConnection { ar ->
if (ar.succeeded()) {
val connection = ar.result()
connection.queryWithParams(sql, params) { res ->
connection.close() //(10) release the connection
if (res.succeeded()) {
val result = res.result()
promise.complete(result)
} else {
promise.fail(res.cause())
}
}
} else {
promise.fail(ar.cause())
}
}
return promise.future()
}
我可以获取里面的数据if (res.succeeded())
。
我的问题是:为什么我们可以在迭代获取数据之前关闭并释放连接?它是如何工作的?
queryWithParams
API 在执行时从数据库中获取整个响应。不会延迟获取结果。因此,在响应处理程序回调开始时关闭连接是安全的,因为到那时客户端已经接收到整个结果集。只有当您使用 queryStream
API 时才会延迟获取结果。如果您正在使用 API,您可能希望等到所有结果都收到后才关闭连接。