How to keep Snowflake connection alive in python

Viewed 476

I have stored my database in the Snowflake server and using https://pypi.org/project/snowflake-connector-python/ I am accessing the database and doing database queries from flask server as below:

ctx = snowflake.connector.connect(
user='username',
password='pass',
account='account',
client_session_keep_alive=True
)
cs = ctx.cursor()

try:
    cs.execute("SELECT current_version()")
    one_row = cs.fetchone()
    print("Successfully connected to snowflake version: {}".format(one_row[0]))
    cs.close()
except Exception as e:
    print("Snowflake connection error: {}".format(e))
    cs.close()

I have defined the snowflake connection session as a global ctx variable to be accessible from any flask api function.

After starting the flask server, everything works fine but if no api calls are made for continuously few hours then it throws an error 'snowflake.connector.errors.ProgrammingError: 390114 (08001): Authentication token has expired. The user must authent icate again.'

As you can see i have kept the 'client_session_keep_alive=True' parameter in the snowflake connect api to keep the session alive but still somehow this fails. I explored about this issue but did not get any conclusive information. So i want to know how can i keep the database connection session alive or do I have to create new connection session for each query?

Any suggestions will be very helpfull.

0 Answers
Related