def close_connection(exception): db = getattr(g, '_database', None) if db is not None: db.disconnect()