From 65c870189e09ec4c8425c65e94d545f4f4cd2563 Mon Sep 17 00:00:00 2001 From: Brandon Madriz Date: Mon, 2 Aug 2021 10:43:20 -0600 Subject: [PATCH] Too many connections - Solution --- climmob/config/celery_class.py | 10 ++++++---- climmob/models/repository.py | 8 ++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/climmob/config/celery_class.py b/climmob/config/celery_class.py index 2559634a..91f0ab2f 100644 --- a/climmob/config/celery_class.py +++ b/climmob/config/celery_class.py @@ -1,21 +1,23 @@ from celery import Task from sqlalchemy import create_engine from climmob.config.celery_app import get_ini_value - +from sqlalchemy.pool import NullPool from celery.contrib.abortable import AbortableTask class celeryTask(AbortableTask): def on_success(self, retval, task_id, args, kwargs): - engine = create_engine(get_ini_value("sqlalchemy.url")) - engine.execute( + engine = create_engine(get_ini_value("sqlalchemy.url"), poolclass=NullPool) + connection = engine.connect() + connection.execute( "INSERT INTO finishedtasks(taskid,taskerror) VALUES ('" + str(task_id) + "',0)" ) + connection.invalidate() engine.dispose() def on_failure(self, exc, task_id, args, kwargs, einfo): - engine = create_engine(get_ini_value("sqlalchemy.url")) + engine = create_engine(get_ini_value("sqlalchemy.url"), poolclass=NullPool) engine.execute( "INSERT INTO finishedtasks(taskid,taskerror) VALUES ('" + str(task_id) diff --git a/climmob/models/repository.py b/climmob/models/repository.py index 4c1ceb9a..fd581546 100644 --- a/climmob/models/repository.py +++ b/climmob/models/repository.py @@ -1,9 +1,9 @@ from sqlalchemy import create_engine from climmob.config.celery_app import get_ini_value - +from sqlalchemy.pool import NullPool def sql_fetch_one(sql): - engine = create_engine(get_ini_value("sqlalchemy.url")) + engine = create_engine(get_ini_value("sqlalchemy.url"), poolclass=NullPool) connection = engine.connect() res = connection.execute(sql).fetchone() connection.invalidate() @@ -12,7 +12,7 @@ def sql_fetch_one(sql): def sql_fetch_all(sql): - engine = create_engine(get_ini_value("sqlalchemy.url")) + engine = create_engine(get_ini_value("sqlalchemy.url"), poolclass=NullPool) connection = engine.connect() res = connection.execute(sql).fetchall() connection.invalidate() @@ -21,7 +21,7 @@ def sql_fetch_all(sql): def sql_execute(sql): - engine = create_engine(get_ini_value("sqlalchemy.url")) + engine = create_engine(get_ini_value("sqlalchemy.url"), poolclass=NullPool) connection = engine.connect() res = connection.execute(sql) connection.invalidate()