1

What's the best practice for storing/re-using the ibis database connection between functions in python?

Currently, I'm using a function to connect to the db that looks like this:

def get_ibis_db(app):
try:
    connection = ibis.mysql.connect(url=app.config.get('DATABASE_URI'))
except ConnectionError as e:
    log.error("Database error for Ibis connection")
    raise e
return connection

...that I call before each query to create the connection. For example:

def my_query():
  ibis_connection = get_ibis_db(app)
  data = ibis_connection.table('mytable')

But, I have several functions that build various dashboard widget reports on the same page, so it looks like a new database connection is being created for each one.

I'm wondering if there is a way to define the connection globally, similar to how Flask-SQLAlchemy does, and have all the functions share it. Is that possible?

Progman
  • 16,827
  • 6
  • 33
  • 48

0 Answers0