0

データベースへの接続とアドホックなデータのクエリを利用するshinyアプリがあります。RPostgreSQL

# Initialize environment to hold SQL parameters:
library(RPostgreSQL)
if (!exists('.sql')) .sql <- new.env()
.sql$cxn <- dbConnect(PostgreSQL(), host = "localhost", dbname = "testdb", user = "myuser", password = "Passw0rd!", port = 5432)

このコードはアプリの初期化時に実行されますが、しばらくするとサーバーによって接続が終了されます。

> dbGetQuery(.sql$cxn, "SELECT 1")
Error in postgresqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not run statement: no connection to the server
)

私が単に呼び出すと:

.sql$cxn <- dbConnect(PostgreSQL(), host = "localhost", dbname = "testdb", user = "myuser", password = "Passw0rd!", port = 5432)

繰り返しますが、2番目の接続が作成されます:(そして、私はそれを望んでいません)

> dbListConnections(PostgreSQL())
[[1]]
<PostgreSQLConnection:(10601,4)> 

[[2]]
<PostgreSQLConnection:(10601,5)> 

(最終的に、接続の最大数に達し、別の接続を作成できなくなります: https://groups.google.com/forum/#!topic/shiny-discuss/0VjQc2a6z3M )

PostgreSQL データベースに接続する関数を作成し (まだ接続されていない場合)、次の場合は接続を開いたままにします (1 を選択することにより)。

getConnection <- function(.host, .user, .pw) {
  tryCatch({
    if (!exists('cxn', where = .sql)) {
      .sql$cxn <- dbConnect(PostgreSQL(), host = .host, dbname = "testdb", user = .user, password = .pw, port = 5432)
    } else {
      dbGetQuery(.sql$cxn, "SELECT 1")
    }
  }, warning = function(w) {
    NULL  # placeholder for warnings
  }, error = function(e) {
    print(e)
    cat("Looks like PostgreSQL connection died. Let's try to reconnect...\n")
    invisible(lapply(dbListConnections(PostgreSQL()), dbDisconnect))  # Close all db connections
.sql$cxn <<- dbConnect(PostgreSQL(), host = .host, dbname = "testdb", user = .user, password = .pw, port = 5432)
  }, finally = {
    return(.sql$cxn)
  })
}

getConnection(.host = "localhost", .user = "myuser", .pw = "Passw0rd!")

EDIT 2016/03/12:理由はわかりませんが、上に書いた関数が正しく動作していないようです...

それを呼び出すと、次のようになります。

> getConnection(.host = awsrds$host, .user = awsrds$username, .pw = awsrds$password)
Error in postgresqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not run statement: no connection to the server
)
<PostgreSQLConnection:(12495,0)> 

特に、この部分は以下をdbGetQuery(.sql$cxn, "SELECT 1")返します。

Error in postgresqlExecStatement(conn, statement, ...) : 
  RS-DBI driver: (could not run statement: no connection to the server
)
NULL
Warning message:
In postgresqlQuickSQL(conn, statement, ...) :
  Could not create executeSELECT 1

そして、classこの出力の はNULL(エラーではなく?) です。

私が間違っていることはありますか?ありがとう!

4

1 に答える 1

-2

接続機能にチェックを入れることでエラーを解決できると思います。これは私がmysqlに対して行ったことです...あなたの場合も同じように機能するはずです。重要なのは、次のことを確認することですSELECT 1 == 'try-error'

connectionFunction <- function() {
  if (!exists("connectionName", where = .GlobalEnv)) {
      connectionName <<- dbConnect(MySQL(), default.file = mysqlconf, dbname = "dbName")
  } else if (class(try(dbGetQuery(connectionName, "SELECT 1"))) == "try-error") {
      dbDisconnect(connectionName)
      connectionName <<- dbConnect(MySQL(), default.file = mysqlconf, dbName = "dbName")
  }
  return(connectionName)

}

于 2016-03-01T15:30:48.763 に答える