Skip to content

Commit

Permalink
fix: use context manager to connect to database
Browse files Browse the repository at this point in the history
  • Loading branch information
benoitblanc committed Apr 18, 2024
1 parent 7e9f785 commit accfc55
Showing 1 changed file with 11 additions and 9 deletions.
20 changes: 11 additions & 9 deletions search_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,18 @@ def search(self, searchtext):
)
)
self.logger.debug(f"SQL Query : {sql}")
conn = self.db_engine.db_engine(document["db_url"]).connect()
results[key] = []
try:
result = conn.execute(sql)
for row in result:
row_result = self._feature_from_query(row, document["primary_key"])
results[key].append(row_result)
self.logger.debug(f"SQL Result for document {key} : {results[key]}")
except Exception as e:
self.logger.error(f"Error for document {key} on query {sql}: {e}")
with self.db_engine.db_engine(document["db_url"]).connect() as conn:
try:
result = conn.execute(sql)
for row in result:
row_result = self._feature_from_query(
row, document["primary_key"]
)
results[key].append(row_result)
self.logger.debug(f"SQL Result for document {key} : {results[key]}")
except Exception as e:
self.logger.error(f"Error for document {key} on query {sql}: {e}")
return results

def _feature_from_query(self, row, primary_key):
Expand Down

0 comments on commit accfc55

Please sign in to comment.