Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mitigate proliferation of idle connections: SqlAlchemy Con --> Engine #729

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions backend/db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,41 @@ def refresh_derived_tables(
break


ENGINES = {
# 'n3c': get_engine(),
# 'public': get_engine(schema='public')
}


def get_engine(isolation_level='AUTOCOMMIT', schema: str = SCHEMA, local=False):
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_engine()

@Sigfried Gonna just leave the work we did in this open PR. This is a low priority issue now that we've resolved the phone call in the house and also got an action that closes extra idle connections.

"""Connect to db
:param local: If True, connection is on local instead of production database."""
engine = ENGINES.get(schema, '') or create_engine(get_pg_connect_url(local), isolation_level=isolation_level)
if schema not in ENGINES:
ENGINES[schema] = engine

# noinspection PyUnusedLocal
@event.listens_for(engine, "connect", insert=True)
def set_search_path(dbapi_connection, connection_record):
"""This does "set search_path to n3c;" when you connect.
https://docs.sqlalchemy.org/en/14/dialects/postgresql.html#setting-alternate-search-paths-on-connect
:param connection_record: Part of the example but we're not using yet.

Ideally, we'd want to be able to call this whenever we want. But cannot be called outside of context of
initializing a connection.
"""
if not schema:
return
existing_autocommit = dbapi_connection.autocommit
dbapi_connection.autocommit = True
cursor = dbapi_connection.cursor()
cursor.execute(f"SET SESSION search_path='{schema}'")
cursor.close()
dbapi_connection.autocommit = existing_autocommit

return engine


# todo: make 'isolation_level' the final param, since we never override it. this would it so we dont' have to pass the
# other params as named params.
def get_db_connection(isolation_level='AUTOCOMMIT', schema: str = SCHEMA, local=False) -> Connection:
Expand Down
13 changes: 10 additions & 3 deletions backend/routes/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from backend.api_logger import Api_logger, get_ip_from_request
from backend.db.refresh import refresh_db
from backend.db.queries import get_concepts
from backend.db.utils import get_db_connection, sql_query, SCHEMA, sql_query_single_col, sql_in, sql_in_safe, run_sql
from backend.db.utils import get_db_connection, get_engine, sql_query, SCHEMA, sql_query_single_col, sql_in, \
sql_in_safe, run_sql
from backend.utils import return_err_with_trace, commify
from enclave_wrangler.config import RESEARCHER_COLS
from enclave_wrangler.models import convert_rows
Expand Down Expand Up @@ -597,8 +598,14 @@ def n3c_comparison_rpt():
display comparison data compiled in generate_n3c_comparison_rpt()
and get_comparison_rpt()
"""
with get_db_connection() as con:
engine = get_engine(schema='public')
with engine.connect() as con:
# with get_db_connection(schema='public') as con:
rpt = sql_query_single_col(con, "SELECT rpt FROM public.codeset_comparison WHERE rpt IS NOT NULL")
print()
# con.close()
engine.dispose()
print()
return rpt


Expand All @@ -609,7 +616,7 @@ def single_n3c_comparison_rpt(pair: str):
and get_comparison_rpt()
"""
orig_codeset_id, new_codeset_id = pair.split('-')
with get_db_connection() as con:
with get_db_connection(schema='public') as con:
rpt = sql_query_single_col(
con,
"SELECT rpt FROM public.codeset_comparison WHERE orig_codeset_id || '-' || new_codeset_id = :pair",
Expand Down
Loading