Search code examples
pythonsnowflake-cloud-data-platformsnowflake-connector

How do I check the status of my Snowflake connection in Python?


I'm using python to query data from snowflake. First I create a connector using the following code -

con = snowflake.connector.connect(
      user = myusername,
      pass = mypassword,
      account = accountname

)

when I run con, I get <snowflake.connector.connection.SnowflakeConnection at 0x240a2e8b8b0>. However even after I run con.close(), when I run con I still get <snowflake.connector.connection.SnowflakeConnection at 0x240a2e8b8b0>, even though the connection is closed.

How do I check the status of con to determine if it is open or closed?


Solution

  • <snowflake.connector.connection.SnowflakeConnection at 0x240a2e8b8b0> is the default repr which is printing you Connection, as the name of the object snowflake.connector.connection.SnowflakeConnection and the memory reference (which allows telling two same typed objects part).

    So given your object is a Connection, it's not going to become invalid from a python object level, because the underlying connection is closed, or had exceptions etc etc.

    The Connection object description does not mention any "status" properties.

    Generally the status is not needed. You should be able to close it many time with no side effect, if that is a concern. And if you have not used it for a while and it has timed-out, when you use the connection, it should throw an error, which you handle and re-try, and if the error is a type the indicates "the connection is not valid" you make a new connection first.