断开dplyr中的src_tbls连接

时间:2014-10-12 23:49:52

标签: sql r dplyr

有没有办法强制断开与src_tbls相似的dplyr 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命令。

1 个答案:

答案 0 :(得分:10)

你可以这样做:

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() } 源文件)调用垃圾收集的内容:

{{1}}