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

Revert SharingPool usage #8081

Merged
merged 3 commits into from
May 12, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
3 changes: 3 additions & 0 deletions kolibri/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ def global_fixture():
if not os.path.exists(os.path.join(TEMP_KOLIBRI_HOME, "content")):
os.mkdir(os.path.join(TEMP_KOLIBRI_HOME, "content"))
yield # wait until the test ended
from kolibri.core.tasks.main import connection

connection.dispose()
if os.path.exists(TEMP_KOLIBRI_HOME):
try:
shutil.rmtree(TEMP_KOLIBRI_HOME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
)
from kolibri.core.content.utils.sqlalchemybridge import get_default_db_string
from kolibri.core.content.utils.sqlalchemybridge import prepare_base
from kolibri.core.content.utils.sqlalchemybridge import SharingPool
from kolibri.core.content.utils.sqlalchemybridge import SQLALCHEMY_CLASSES_PATH_TEMPLATE
from kolibri.core.utils.sqlalchemy_utils import SharingPool

DATA_PATH_TEMPLATE = os.path.join(
os.path.dirname(__file__), "../../fixtures/{name}_content_data.json"
Expand Down
2 changes: 1 addition & 1 deletion kolibri/core/content/test/sqlalchemytesting.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from sqlalchemy import create_engine

from kolibri.core.content.utils.sqlalchemybridge import get_default_db_string
from kolibri.core.utils.sqlalchemy_utils import SharingPool
from kolibri.core.content.utils.sqlalchemybridge import SharingPool


def django_connection_engine():
Expand Down
40 changes: 40 additions & 0 deletions kolibri/core/content/utils/sqlalchemybridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,46 @@ class ClassNotFoundError(Exception):
pass


# get_conn and SharingPool code modified from:
# http://nathansnoggin.blogspot.com/2013/11/integrating-sqlalchemy-into-django.html


def get_conn(self):
"""
custom connection factory, so we can share with django
"""
from django.db import connections

conn = connections["default"]
return conn.connection


class SharingPool(NullPool):
"""
custom connection pool that doesn't close connections, and uses our
custom connection factory
"""

def __init__(self, get_connection, **kwargs):
kwargs["reset_on_return"] = False
super(SharingPool, self).__init__(get_conn, **kwargs)

def status(self):
return "Sharing Pool"

def _do_return_conn(self, conn):
pass

def _do_get(self):
return self._create_connection()

def _close_connection(self, connection):
pass

def dispose(self):
pass


def sqlite_connection_string(db_path):
# Call normpath to ensure that Windows paths are properly formatted
return "sqlite:///{db_path}".format(db_path=os.path.normpath(db_path))
Expand Down
7 changes: 0 additions & 7 deletions kolibri/core/tasks/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
from kolibri.core.tasks.queue import Queue
from kolibri.core.tasks.scheduler import Scheduler
from kolibri.core.tasks.worker import Worker
from kolibri.core.utils.sqlalchemy_utils import SharingPool
from kolibri.utils import conf


Expand All @@ -28,8 +27,6 @@ def __create_engine():
path=os.path.join(conf.KOLIBRI_HOME, "job_storage.sqlite3")
),
connect_args={"check_same_thread": False},
# Use NullPool for SQLite as we use a completely separate database
# file, so no need to share anything with Django.
poolclass=NullPool,
)

Expand All @@ -49,10 +46,6 @@ def __create_engine():
),
pool_pre_ping=True,
client_encoding="utf8",
# Use our SharingPool for Postgres, so as to ensure that
# we share underlying database connections with Django
# this results in cleaner shutdown and clean up of connections
poolclass=SharingPool,
)


Expand Down
39 changes: 0 additions & 39 deletions kolibri/core/utils/sqlalchemy_utils.py

This file was deleted.