Created
March 6, 2024 17:14
-
-
Save tejas-kr/ab3ba20e7e2c635e251a489dae28ed87 to your computer and use it in GitHub Desktop.
Flask DB Connection Manager Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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