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

close django connection in REST API #4741

Closed
wants to merge 4 commits into from
Closed
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
18 changes: 14 additions & 4 deletions aiida/backends/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,22 @@
# For further information please visit http://www.aiida.net #
###########################################################################
"""Backend-agnostic utility functions"""
from sqlalchemy.pool import NullPool
from aiida.backends import BACKEND_SQLA, BACKEND_DJANGO
from aiida.manage import configuration
from aiida.common import json

AIIDA_ATTRIBUTE_SEP = '.'

SQLALCHEMY_ENGINE_DEFAULTS = dict(
json_serializer=json.dumps,
json_deserializer=json.loads,
encoding='utf-8',
poolclass=NullPool,
# pool_size=1,
# max_overflow=0,
)


def create_sqlalchemy_engine(profile, **kwargs):
"""Create SQLAlchemy engine (to be used for QueryBuilder queries)
Expand All @@ -22,7 +33,6 @@ def create_sqlalchemy_engine(profile, **kwargs):
more info.
"""
from sqlalchemy import create_engine
from aiida.common import json

# The hostname may be `None`, which is a valid value in the case of peer authentication for example. In this case
# it should be converted to an empty string, because otherwise the `None` will be converted to string literal "None"
Expand All @@ -37,9 +47,9 @@ def create_sqlalchemy_engine(profile, **kwargs):
port=profile.database_port,
name=profile.database_name
)
return create_engine(
engine_url, json_serializer=json.dumps, json_deserializer=json.loads, encoding='utf-8', **kwargs
)
kwargs_new = SQLALCHEMY_ENGINE_DEFAULTS.copy()
kwargs_new.update(kwargs)
return create_engine(engine_url, **kwargs_new)


def create_scoped_session_factory(engine, **kwargs):
Expand Down
8 changes: 7 additions & 1 deletion aiida/restapi/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -857,4 +857,10 @@ def close_session(wrapped, _, args, kwargs):
try:
return wrapped(*args, **kwargs)
finally:
get_manager().get_backend().get_session().close()
backend = get_manager().get_backend()
backend.get_session().close()

# Close django connection if open (not needed by REST API)
if 'Django' in type(backend).__name__: # don't want to import the backend class, gives error
from django.db import connection
connection.close()
3 changes: 2 additions & 1 deletion tests/restapi/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ def server_url():
def restrict_sqlalchemy_queuepool(aiida_profile):
"""Create special SQLAlchemy engine for use with QueryBuilder - backend-agnostic"""
from aiida.manage.manager import get_manager
from sqlalchemy.pool import QueuePool

backend_manager = get_manager().get_backend_manager()
backend_manager.reset_backend_environment()
backend_manager.load_backend_environment(aiida_profile, pool_timeout=1, max_overflow=0)
backend_manager.load_backend_environment(aiida_profile, poolclass=QueuePool, pool_timeout=1, max_overflow=0)


@pytest.fixture
Expand Down