Skip to content

Commit ee28f6e

Browse files
authored
Merge pull request #2201 from wagner-intevation/fix-220
Fix reconnection in SQL Mixin
2 parents 3b15e09 + 5249d0e commit ee28f6e

File tree

1 file changed

+20
-16
lines changed

1 file changed

+20
-16
lines changed

intelmq/lib/mixins/sql.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
""" SQLMixin for IntelMQ
22
3-
SPDX-FileCopyrightText: 2021 Birger Schacht
3+
SPDX-FileCopyrightText: 2021 Birger Schacht, 2022 Intevation GmbH
44
SPDX-License-Identifier: AGPL-3.0-or-later
55
66
Based on the former SQLBot base class
77
"""
8+
from intelmq.lib import exceptions
89

910

1011
class SQLMixin:
@@ -18,36 +19,39 @@ class SQLMixin:
1819

1920
POSTGRESQL = "postgresql"
2021
SQLITE = "sqlite"
21-
default_engine = "postgresql"
22+
_default_engine = "postgresql"
2223
engine = None
2324
# overwrite the default value from the OutputBot
2425
message_jsondict_as_string = True
2526

2627
def __init__(self, *args, **kwargs):
28+
self._init_sql()
29+
30+
super().__init__(*args, **kwargs)
31+
32+
def _init_sql(self):
2733
self.logger.debug("Running SQL Mixin initialization.")
28-
self.engine_name = getattr(self, 'engine', self.default_engine).lower()
34+
self._engine_name = getattr(self, 'engine', self._default_engine).lower()
2935
engines = {SQLMixin.POSTGRESQL: (self._init_postgresql, "%s"),
3036
SQLMixin.SQLITE: (self._init_sqlite, "?")}
3137
for key, val in engines.items():
32-
if self.engine_name == key:
38+
if self._engine_name == key:
3339
val[0]()
3440
self.format_char = val[1]
3541
break
3642
else:
37-
raise ValueError(f"Wrong parameter 'engine' {self.engine_name!r}, possible values are {engines}")
38-
39-
super().__init__()
43+
raise ValueError(f"Wrong parameter 'engine' {self._engine_name!r}, possible values are {engines}")
4044

4145
def _connect(self, engine, connect_args: dict, autocommitable: bool = False):
42-
self.engine = engine # imported external library that connects to the DB
46+
self._engine = engine # imported external library that connects to the DB
4347
self.logger.debug(f"Connecting to database with connect_args: {connect_args}.")
4448

4549
try:
46-
self.con = self.engine.connect(**connect_args)
50+
self.con = self._engine.connect(**connect_args)
4751
if autocommitable: # psycopg2 has it, sqlite3 has not
4852
self.con.autocommit = getattr(self, 'autocommit', True) # True prevents deadlocks
4953
self.cur = self.con.cursor()
50-
except (self.engine.Error, Exception):
54+
except (self._engine.Error, Exception):
5155
self.logger.exception('Failed to connect to database.')
5256
self.stop()
5357
self.logger.info("Connected to database.")
@@ -88,24 +92,24 @@ def execute(self, query: str, values: tuple, rollback=False):
8892
# note: this assumes, the DB was created with UTF-8 support!
8993
self.cur.execute(query, values)
9094
self.logger.debug('Done.')
91-
except (self.engine.InterfaceError, self.engine.InternalError,
92-
self.engine.OperationalError, AttributeError):
95+
except (self._engine.InterfaceError, self._engine.InternalError,
96+
self._engine.OperationalError, AttributeError):
9397
if rollback:
9498
try:
9599
self.con.rollback()
96100
self.logger.exception('Executed rollback command '
97101
'after failed query execution.')
98-
except self.engine.OperationalError:
102+
except self._engine.OperationalError:
99103
self.logger.exception('Executed rollback command '
100104
'after failed query execution.')
101-
self.init()
105+
self._init_sql()
102106
except Exception:
103107
self.logger.exception('Cursor has been closed, connecting '
104108
'again.')
105-
self.init()
109+
self._init_sql()
106110
else:
107111
self.logger.exception('Database connection problem, connecting again.')
108-
self.init()
112+
self._init_sql()
109113
else:
110114
return True
111115
return False

0 commit comments

Comments
 (0)