title: Check if odbc connection is open or closed
body: <p>I want to write a function that runs the following operation: "if a database connection is already open, close it and reopen a new one"</p>
  
  <p>To do this I need a way to check if the connection is already open</p>
  
  <p>Here is my connection string:</p>
  
  <pre><code>library(odbc)
  
  my_conn_string &lt;- paste("Driver{Teradata};DBCName=teradata2690;DATABASE=PRODUCTION;UID=",
                              username,";PWD=",password, sep="")
  
  t2690 &lt;- dbConnect(odbc::odbc(), .connection_string=my_conn_string)
  </code></pre>
  
  <p>I thought about using the <code>class</code> of the connection:</p>
  
  <pre><code>    if (is.null(class(t2690)) { 
  t2690 &lt;- dbConnect(odbc::odbc(), .connection_string=my_conn_string) 
  }
  </code></pre>
  
  <p>However this does not work as the class is the same whether the connection is open or not:</p>
  
  <pre><code># Class when connected
  class(t2690)
  
  [1] "Teradata"
  attr(,"package")
  [1] ".GlobalEnv"
  
  # Class when not connected
  dbDisconnect(t2690)
  class(t2690)
  
  [1] "Teradata"
  attr(,"package")
  [1] ".GlobalEnv"
  </code></pre>