Skip to content

Commit 1378c4e

Browse files
committed
Added better connection handling on the database
1 parent 46e9b2e commit 1378c4e

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

examples/8-url-loader.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
username=os.environ.get("MEMGRAPH_USERNAME", "memgraph"),
1717
password=os.environ.get("MEMGRAPH_PASSWORD", "memgraph"),
1818
)
19+
db.connect()
1920

2021
loader = WebLoader("http://localhost:4000/en/enterprise-cloud@latest/copilot")
2122
embedder = TextEmbedding3Small()
@@ -39,6 +40,8 @@ async def main():
3940
vectors = []
4041
await embedder.process_chunks(chunks, callback=lambda v: vectors.append(v))
4142
store(source, doc, chunks, vectors)
43+
44+
db.close()
4245

4346

4447
if __name__ == "__main__":

src/core/rag/dbhandler/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,6 @@ def __init__(
4343
self._conn = None
4444
self._cur = None
4545

46-
try:
47-
self.connect(**kwargs)
48-
except Exception as e:
49-
print(f"Connection error: {str(e)}")
50-
raise ConnectionError(f"Failed to connect to Memgraph at {host}:{port}: {str(e)}") from e
51-
5246
def connect(self, *args, **kwargs) -> None:
5347
raise ConnectionError("Not implemented")
5448

@@ -57,7 +51,13 @@ def close(self) -> None:
5751
self._conn.close()
5852

5953
# Context-manager support
60-
def __enter__(self) -> GraphClient:
54+
def __enter__(self, *args, **kwargs) -> GraphClient:
55+
try:
56+
self.connect(**kwargs)
57+
except Exception as e:
58+
print(f"Connection error: {str(e)}")
59+
raise ConnectionError(f"Failed to connect to Memgraph at {self.host}:{self.port}: {str(e)}") from e
60+
6161
return self
6262

6363
def __exit__(self, exc_type, exc, tb) -> None:

0 commit comments

Comments
 (0)