Sql 断开dplyr中的src_tbls连接
是否有一种方法可以强制断开Sql 断开dplyr中的src_tbls连接,sql,r,dplyr,Sql,R,Dplyr,是否有一种方法可以强制断开dplyr中的src\u 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: (cann
dplyr
中的src\u 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)
之后,您可以再次运行src\u postgres
命令。您可以执行以下操作:
RPostgreSQL::dbDisconnect(src_temp$con)
强行断开连接
这就是在dplyr
中通过该函数对垃圾收集的调用(通过dbi-s3.r
源文件):
#创建一个在数据库运行时断开数据库连接的环境
#垃圾收集
db_隔离开关换句话说,您只需rm()
旧连接,它们将在下一次gc时消失。这对我来说是不会发生的:在rm(list=ls())
之后,我仍然在postgresqlNewConnection(drv,…)中得到错误:RS-DBI驱动程序:(�P�_�无法分配新连接(最多16个已打开的连接)
当我尝试打开新的dplyr::src_postgres(…)
@Dambo时,现在我只需关闭并重新打开项目。这很可靠。@Gabi,因为上面的dplyr版本@Dambo。
RPostgreSQL::dbDisconnect(src_temp$con)
# 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()
}