有没有办法强制断开dplyr
中的src_tbls
对象,类似于RPostgreSQL::dbDisconnect
?
参见示例:
> src_temp <- src_postgres(dbname = "temp", host = "127.0.0.1", port = 5432, user = "x", password = "y")
Error in postgresqlNewConnection(drv, ...) :
RS-DBI driver: (cannot allocate a new connection -- maximum of 16 connections already opened)
顺便说一句,它会在几秒钟后很快自动断开连接:
Auto-disconnecting postgres connection (3734, 26)
之后您可以再次运行src_postgres
命令。
最佳答案
你可以这样做:
RPostgreSQL::dbDisconnect(src_temp$con)
强制断开连接。
这就是 dplyr
中此函数调用垃圾收集的内容(通过 dbi-s3.r
源文件):
# Creates an environment that disconnects the database when it's
# garbage collected
db_disconnector <- function(con, name, quiet = FALSE) {
reg.finalizer(environment(), function(...) {
if (!quiet) {
message("Auto-disconnecting ", name, " connection ",
"(", paste(con@Id, collapse = ", "), ")")
}
dbDisconnect(con)
})
environment()
}
关于sql - 断开 dplyr 中的 src_tbls 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26331201/