r - Why am I getting warnings about closing unused RODBC handles? -
i use rodbc r , knitr reporting using various production databases. in of these reports i'm running multiplie queries against multiple databases.
each of queries carried out in function of form:
get.total.orders <- function(db.connex.string, start.date, end.date){ db.connex <- odbcdriverconnect(db.connex.string) ord.qry <- sprintf("select ord_orderreference 'order.ref', ord_registerdate 'register.date' orders ord_registerdate >= '%s' , ord_registerdate < '%s'", start.date, end.date) orders <- sqlquery(db.connex, ord.qry) odbcclose(db.connex) return(orders) } note odbc channel opened , closed in function, , single, simple query run between opening , closing of channel.
nonetheless, when run report more once (e.g. when developing report), receive warnings such following:
warning: closing unused rodbc handle 41 the more times run report, higher number of handle reported in error becomes.
why, if i'm opening , closing channel in query function, being left open, 'unused' rodbc handles?
more importantly, how can avoid issue?
i avoid using on.exit:
get.total.orders <- function(db.connex.string, start.date, end.date){ db.connex <- odbcdriverconnect(db.connex.string) on.exit(odbcclose(db.connex)) # <----------------------- change here ord.qry <- sprintf("select ord_orderreference 'order.ref', ord_registerdate 'register.date' orders ord_registerdate >= '%s' , ord_registerdate < '%s'", start.date, end.date) orders <- sqlquery(db.connex, ord.qry) return(orders) } this way, connection closed if there error. see ?on.exit.
[edit]
the above presumes handle not closed because there error executing query. if query ok handle wasn't closed have no idea. odbcclose returns 0 if succeeded might check that.
[edit2]
as others have pointed out, nothing worry - on other hand, still interesting figure out why connection not closed if explicitly tell close. maybe matter of milliseconds , query not yet finished when result being assigned. doesn't make sense me if result assigned orders else there database? maybe there something. in case, 1 might try give more time eg.
#... orders <- sqlquery(db.connex, ord.qry) orders # or force(orders) - evaluate result once more sys.sleep(0.01) # give 10 milliseconds orders # or return(orders) - return result # presuming on.exit before - odbcclose happen here } this sounds stupid wouldn't surprised if work.
another idea if using rstudio may phantom error messages happens example, when using plot non-existing graphical parameter fist time, , no errors on second time.
plot(1, bimbo=2) # here warnings bimbo not graphical parameter plot(2) # nothing wrong here rstudio replays previous warnings maybe similar happens db handlers -- if case, instructive see if same warnings in both rstudio , console (rgui or rterm in windows or running r in terminal in linux). of course applies if using rstudio.
and finally, might try posting on r-help brian ripley (one of authors of rodbc) there not here.
so see, don't have real answer , if takes effort figure out, recommend not worrying :)
Comments
Post a Comment