title: How does R handle closing of data base connections
body: <p>If I create a data base connection within a function, the connection objects gets destroyed when the function finished executing. Does this reliably close the data base connection, or would it better to to close it manually first? </p>
  
  <p><strong>Why I need to know this:</strong></p>
  
  <p>I am working on a package that creates data base connections on the fly with either RODBC or RJDBC as backend. I designed my function interfaces so that you can pass in either username and password, or a connection object. Usually, when I pass in a connection object I do not want the connection to be closed on the termination of the function, while when I pass in username and password I want it to be closed. </p>
  
  <p>If I do not have to worry about open connections, it would simplify things a lot for me and also save me a lot of headaches.</p>
  
  <p><strong>Answer &amp; More:</strong></p>
  
  <p>I markded Benjamin's answer as the answer since it gives good advice, though actually what I was looking for is more Marek's comment that (paraphrased:) <strong>connections can be open after the connection object is destroyed and there is no way to access them from R any more.</strong></p>
  
  <p>I ended up going for a solution that involves creating an R6 class and defining <code>finalize()</code> method that closes the connection (it's a more powerful than <code>on.exit()</code>), but that is beyond the scope of this Question.</p>