Project

General

Profile

Bug #4569 » 0002-Fix-Fixed-enricher-failure-on-DB-restart.patch

Radko Krkoš, 05/21/2019 12:32 PM

View differences:

lib/mentat/services/sqlstorage.py
27 27

  
28 28
import copy
29 29
import sqlalchemy
30
from sqlalchemy.orm import Query
30 31

  
31 32
#
32 33
# Custom libraries
......
38 39
_MANAGER = None
39 40

  
40 41

  
42
class RetryingQuery(Query):
43
    """
44
    An override of SQLAlchemy's Query class, allowing for recovery from a lost DB
45
    connection.
46
    """
47
    def _execute_and_instances(self, querycontext):
48
        for _ in range(2):
49
            try:
50
                return super()._execute_and_instances(querycontext)
51
            except sqlalchemy.exc.OperationalError:
52
                self.session.close()
53
                continue
54

  
55

  
41 56
class StorageService:
42 57
    """
43 58
    Proxy object for working with persistent SQL storages. Maintains and provides
......
54 69
        """
55 70
        self.dbengine     = sqlalchemy.engine_from_config(enginecfg, prefix = '')
56 71
        self.sessionmaker = sqlalchemy.orm.sessionmaker(bind = self.dbengine)
57
        self.session      = self.sessionmaker()
72
        self.session      = self.sessionmaker(query_cls = RetryingQuery)
58 73

  
59 74
    def __del__(self):
60 75
        self.close()
(2-2/4)