Skip to content
Snippets Groups Projects
Commit 0396d2ef authored by Rajmund Hruška's avatar Rajmund Hruška
Browse files

Merge branch 'hruska-feature-#6205-sqlalchemy-v2' into devel

parents 8a33c6f5 4286f394
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,8 @@ ...@@ -10,7 +10,8 @@
""" """
Database storage abstraction layer. The current implementation is based on the Database storage abstraction layer. The current implementation is based on the
awesome `SQLAlchemy <http://www.sqlalchemy.org/>`__ library. awesome `SQLAlchemy <http://www.sqlalchemy.org/>`__ library accessed by
`flask_sqlalchemy <https://flask-sqlalchemy.palletsprojects.com/>`__.
.. warning:: .. warning::
...@@ -26,8 +27,8 @@ __credits__ = "Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea ...@@ -26,8 +27,8 @@ __credits__ = "Pavel Kácha <pavel.kacha@cesnet.cz>, Andrea Kropáčová <andrea
import copy import copy
import sqlalchemy from flask import Flask
from sqlalchemy.orm import Query import flask_sqlalchemy
# #
# Custom libraries # Custom libraries
...@@ -39,7 +40,7 @@ from mentat.datatype.sqldb import MODEL ...@@ -39,7 +40,7 @@ from mentat.datatype.sqldb import MODEL
_MANAGER = None _MANAGER = None
class RetryingQuery(Query): class RetryingQuery(flask_sqlalchemy.orm.Query):
""" """
An override of SQLAlchemy's Query class, allowing for recovery from a lost DB An override of SQLAlchemy's Query class, allowing for recovery from a lost DB
connection. connection.
...@@ -48,7 +49,7 @@ class RetryingQuery(Query): ...@@ -48,7 +49,7 @@ class RetryingQuery(Query):
for _ in range(2): for _ in range(2):
try: try:
return super()._execute_and_instances(querycontext) return super()._execute_and_instances(querycontext)
except sqlalchemy.exc.OperationalError: except flask_sqlalchemy.orm.exc.sa_exc.OperationalError:
self.session.close() self.session.close()
continue continue
...@@ -67,9 +68,14 @@ class StorageService: ...@@ -67,9 +68,14 @@ class StorageService:
:param enginecfg: Connection arguments. :param enginecfg: Connection arguments.
""" """
self.dbengine = sqlalchemy.engine_from_config(enginecfg, prefix = '') self.app = Flask(__name__)
self.sessionmaker = sqlalchemy.orm.sessionmaker(bind = self.dbengine) self.app.config['SQLALCHEMY_DATABASE_URI'] = enginecfg['url']
self._session = self.sessionmaker(query_cls = RetryingQuery) self.app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
self.db = flask_sqlalchemy.SQLAlchemy(self.app)
self.dbengine = self.db.engine_from_config(enginecfg, prefix = '')
self.sessionmaker = flask_sqlalchemy.orm.sessionmaker(bind = self.dbengine)
self._session = self.sessionmaker(query_cls = RetryingQuery)
def __del__(self): def __del__(self):
self.close() self.close()
......
...@@ -270,10 +270,7 @@ class SqldbWhoisModule(WhoisModule): ...@@ -270,10 +270,7 @@ class SqldbWhoisModule(WhoisModule):
}) })
networks.append(netw) networks.append(netw)
# Use rollback to close transaction, please see issue #4251 for details. storage.session.commit()
# In short: In SQLAlchemy commit immediatelly opens new transaction, which
# then keeps hanging in "transaction-idle" state.
storage.session.rollback()
# Let the parent implementation take care of loading internal lookup table. # Let the parent implementation take care of loading internal lookup table.
return super().setup(networks) return super().setup(networks)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment