Skip to content

Instantly share code, notes, and snippets.

@tejas-kr
Created March 6, 2024 17:14
Show Gist options
  • Save tejas-kr/ab3ba20e7e2c635e251a489dae28ed87 to your computer and use it in GitHub Desktop.
Save tejas-kr/ab3ba20e7e2c635e251a489dae28ed87 to your computer and use it in GitHub Desktop.
Flask DB Connection Manager Script
from flask import g, current_app
from db import DB
def get_db():
if 'db' not in g:
g.db = DB()
return g.db
@current_app.teardown_appcontext
def close_db(error):
if error:
logger.exception(error)
db = g.pop('db', None)
if db is not None:
db.conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment