cullan
cullan

Reputation: 280

RMySQL: internal error in RS_DBI_getConnection: corrupt connection handle

So I connected to my database just fine in the beginning:

con <- dbConnect(
MySQL(),
user = "username", password = "password",
host = "<my amazon web service database server>",
port = 3306
)

So everything was working but after a couple runs/refreshes on my Shiny app, it says I have 16 connections open and I can no longer open any connections.

So, I tried running:

dbDisconnect(dbListConnections(MySQL()))

Then I got this:

dbListConnections(MySQL())
[[1]]TRUE

Then I got this error:

Error in .local(dbObj, ...) : 
internal error in RS_DBI_getConnection: corrupt connection handle

Now, whenever I try to open my Shiny app, I get the error on the datatableoutput

(I query the database to generate this output):
internal error in RS_DBI_getConnection: corrupt connection handle

Upvotes: 5

Views: 6803

Answers (1)

NewUser
NewUser

Reputation: 346

I just had the same issue. This is how I solved it:

I was creating a connection and then some functions where using that connection. The thing is, the connection was in the outer scope:

con <- dbConnect(
  MySQL(),
  ...
)

MyFunction <- function(){
  myQuery <- paste("SELECT * FROM ...")
  df <- dbGetQuery(con, myQuery)
  df
}

For me too this worked fine, but stopped working after the "16" connections limit as in your case (same corrupt connection handle error).

Workaround

Create the connection inside of the function and disconnect at the end of the function:

MyFunction <- function(){

  con <- dbConnect(
    MySQL(),
    ...
  )

  myQuery <- paste("SELECT * FROM ...")
  df <- dbGetQuery(con, myQuery)

  dbDisconnect(con)
  df
}

Everything seems to be working now.

Upvotes: 4

Related Questions