Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RMySQL: Closing a connection without a handle

Tags:

mysql

r

rmysql

I am experimenting with RMySQL, and have accidentally created a connection without a handle.

dbConnect(MySQL(), user = "foo", password = "bar")
connLocalDB = dbConnect(MySQL(), user = "foo", password = "bar")

Note that the return of the first call is not assigned to anything. Now, when I do a dbListConnections(MySQL()) I see two connections:

> dbListConnections(MySQL())
[[1]]
<MySQLConnection:0,0>

[[2]]
<MySQLConnection:0,1>

I then tried this:

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

but, then, I got this:

> dbListConnections(MySQL())
[[1]]
Error in .local(dbObj, ...) : 
  internal error in RS_DBI_getConnection: corrupt connection handle

How to safely terminate a connection that is not assigned a handle?

like image 406
tchakravarty Avatar asked Apr 06 '15 06:04

tchakravarty


1 Answers

Generally for creating a connection, getting the data of a query and then closing the connection I use the following function:

getDataSql <- function( query ) {
  con = dbConnect(RMySQL::MySQL(), dbname  =  "dbname", host = "host", user = "username", password = "pasword", port = "port")
  result <- dbGetQuery(con, query)
  dbDisconnect(con)
  result
}
like image 67
mrina713 Avatar answered Sep 28 '22 01:09

mrina713