betodealmeida commented on code in PR #31315: URL: https://github.com/apache/superset/pull/31315#discussion_r2307550428
########## superset/models/core.py: ########## @@ -98,6 +98,48 @@ from superset.models.sql_lab import Query +@contextmanager +def temporarily_disconnect_db(): # type: ignore + """ + Temporary disconnects the metadata database session. + + This is meant to be used during long, blocking operations, so that we can release + the database connection for the duration of, for example, a potentially long running + query against an analytics database. + + The goal here is to lower the number of concurrent connections to the metadata + database, given that Superset has no control over the duration of the + analytics query. + + NOTE: only has an effect if feature flag DISABLE_METADATA_DB_DURING_ANALYTICS + and using NullPool + """ + pool_type = db.engine.pool.__class__.__name__ + # Currently only tested/available when used with NullPool + do_it = ( + is_feature_enabled("DISABLE_METADATA_DB_DURING_ANALYTICS") + and pool_type == "NullPool" + ) + conn = None + try: + if do_it: + conn = db.session.connection() + logger.info("Disconnecting metadata database temporarily") + # Closing the session + db.session.close() + # Closing the connection + conn.close() Review Comment: Why do we need to create a conection on line 126 just to close it on line 131? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@superset.apache.org For additional commands, e-mail: notifications-h...@superset.apache.org