我有一个闪亮的应用程序,使用RPostgreSQL
连接到数据库。在应用程序结束时,连接关闭,应该卸载驱动程序,但出现错误,警告我连接未关闭。RPostgreSQL无法关闭连接
的代码看起来是这样的:
# in the app.R file, but not in the server function:
drv <- dbDriver("PostgreSQL")
con <- dbConnect(drv, dbname = "database1",
host = "localhost", port = 5432,
user = "user", password = "pw")
# in the server function:
foo <- dbGetQuery(con, "SELECT * from table1")
# at the end of the server function to disconnect when the app is closed:
session$onSessionEnded(function(){
dbDisconnect(con)
dbUnloadDriver(drv)
})
但是,我得到的错误信息:Error in postgresqlCloseDriver(drv, ...): RS-DBI driver: (There are opened connections -- close them first)
这将显示带有命令dbUnloadDriver(drv)
。
当我手动查找与dbListConnections()
的打开连接时,我得到一个列表,最多有16个与数据库打开的连接。注意,我只使用dbGetQuery
从不dbSendQuery
避免关闭连接。
任何想法?
我必须卸载驱动程序吗? – David
我只是不喜欢这样写道: 库(RODBC) DBConnection的< - odbcDriverConnect(“驱动程序= ODBC SQL Server的驱动程序11;服务器= RSHUELL00193 \\ SQLEXPRESS;数据库= TESTDB; UID =; PWD =; trusted_connection =是)“) initdata < - sqlQuery(dbconnection,paste(”select * from MyTable;“)) odbcClose(channel) 我完全没有问题。 – ryguy7272