使用 R 池包连接到 PostgreSQL 数据库

标签 r postgresql r-dbi rpostgresql plumber

我有一个用 R 构建的 API plumber使用 RPostgreSQL 连接到 PostgreSQL 数据库和 pool (尽管如果我使用的是 Shiny 应用程序,这也适用):

# create the connection pool
pool <- dbPool(
  drv = PostgreSQL(),
  host = Sys.getenv("DB_HOST"),
  port = 5432,
  dbname = "db",
  user = Sys.getenv("DB_USER"),
  password = Sys.getenv("DB_PASSWORD")
)

# start the API
pr <- plumb("plumber.R")

# on stop, close the pool
pr$registerHooks(
  list("exit" = function() { poolClose(pool) })
)

我想每天导入新数据。最简单的方法是创建一个新数据库并将其推广到生产环境:

CREATE DATABASE db_new;
-- create the tables
-- bulk-insert the data
SELECT pg_terminate_backend (pid) FROM pg_stat_activity WHERE datname = 'db';
DROP DATABASE db;
ALTER DATABASE db_new RENAME TO db;

这速度很快,并且可以最大限度地减少停机时间。问题是 pool 然后失去与数据库的连接并且不会自动尝试重新连接:

> tbl(pool, "users")
Error in postgresqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not Retrieve the result : FATAL:  terminating connection due to administrator command
server closed the connection unexpectedly
    This probably means the server terminated abnormally
    before or while processing the request.
)

即使我不是每天都更换数据库,数据库服务器偶尔也会重启,这也会导致我的应用程序崩溃。重新连接似乎不是池、RPostgreSQL 或 DBI 的功能。有谁知道解决这个问题的方法吗?

最佳答案

我最近遇到了类似的问题,因为当超过实例的 wait_timeout 时 MySQL 连接被关闭。我遇到了your post on RStudio Community ,并受到您的解决方案的启发。如果您仍在使用它,并且正在寻找一种解决方案,在包装您使用的实际功能时避免额外查询,这里有一个 reprex 演示了我想出的东西,以及一个证明它有效的例子:

library(dplyr, warn.conflicts = FALSE)
library(pool)
library(RMariaDB)

generate_safe_query <- function(pool) {
  function(db_function, ...) {
    tryCatch({
      db_function(pool, ...)
    }, error = function(e) {
      if (grepl("Lost connection to MySQL server during query", e$message)) {
        # Preserve `validationInterval` so that it can be restored
        validation_interval <- pool$validationInterval
        # Trigger destruction of dead connection
        pool$validationInterval <- 0
        refreshed_connection <- poolCheckout(pool)
        poolReturn(refreshed_connection)
        # Restore original `validationInterval`
        pool$validationInterval <- validation_interval
        # Execute the query with the new connection
        db_function(pool, ...)
      } else {
        # Unexpected error
        stop(e)
      }
    })
  }
}

mysql_pool <- dbPool(MariaDB(),
                     host = "127.0.0.1",
                     username = "root",
                     password = "",
                     dbname = "test")

safe_query <- generate_safe_query(mysql_pool)

# Works
safe_query(tbl, "notes")
#> # Source:   table<notes> [?? x 2]
#> # Database: mysql 8.0.15 [root@127.0.0.1:/test]
#>      id note 
#>   <int> <chr>
#> 1     1 NOTE1

# Set the `wait_timeout` to 5 seconds for this session
invisible(safe_query(dbExecute, "SET SESSION wait_timeout = 5"))

# Wait longer than `wait_timeout` to trigger a disconnect
Sys.sleep(6)

# Still works; warning will appear notifying that connection was
# destroyed and replaced with a new one
safe_query(tbl, "notes")
#> Warning: It wasn't possible to activate and/or validate the object. Trying
#> again with a new object.
#> # Source:   table<notes> [?? x 2]
#> # Database: mysql 8.0.15 [root@127.0.0.1:/test]
#>      id note 
#>   <int> <chr>
#> 1     1 NOTE1

safe_query(poolClose)
# Or, equivalently: 
# poolClose(mysql_pool)

reprex package 创建于 2019-05-30 (v0.3.0)

generate_safe_query 返回的函数适用于任何数据库查询函数(例如 dbExecutedbGetQuery 等)。显然,您需要更新它匹配的错误消息以满足您的需要。

我自己也开了Community topic在我认为应该包含在 dbPool 中的一个选项上,这将减少对此类解决方法的需求。

关于使用 R 池包连接到 PostgreSQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54988978/

相关文章:

通过facet_wrap重新排序ggplot barplot x轴

r - R 数据集中的 TextInput 过滤器 Shiny

xml - 使用 XML 包将 TCX 导入 R

sql - 类型日期 : "\N" in postgresql when fetching data 的输入语法无效

r - 在 CentOS 6.5 而非 Windows 7 上使用 RPostgreSQL 和 RJDBC 进行 dbSendQuery 时出错

r - 计算数据帧列中事件组合发生的次数

postgresql - Hibernate executeUpdate() 为返回 * 的 PostgreSQL native 查询抛出异常

sql - PostgreSQL 错误 : query has no destination for result data

sql - 如何在没有默认数据库的连接上使用 dbWriteTable

r - 如何使用 DBI 写入包含日期列的表