55"""ArcadeDB DocumentStore for Haystack 2.x — document storage + vector search via HTTP/JSON API."""
66
77import logging
8+ from contextlib import suppress
89from http import HTTPStatus
910from typing import Any , ClassVar
1011
@@ -95,7 +96,7 @@ def __init__(
9596 self ._recreate_type = recreate_type
9697 self ._create_database = create_database
9798
98- self ._session = requests .Session ()
99+ self ._session : requests .Session | None = None
99100 self ._initialized = False
100101
101102 def to_dict (self ) -> dict [str , Any ]:
@@ -134,10 +135,25 @@ def from_dict(cls, data: dict[str, Any]) -> "ArcadeDBDocumentStore":
134135 init_params [key ] = Secret .from_dict (init_params [key ])
135136 return default_from_dict (cls , data )
136137
138+ def close (self ) -> None :
139+ """
140+ Release the associated synchronous resources.
141+ """
142+ if self ._session is not None :
143+ with suppress (Exception ):
144+ self ._session .close ()
145+ self ._session = None
146+
137147 # ------------------------------------------------------------------
138148 # HTTP helpers
139149 # ------------------------------------------------------------------
140150
151+ def _ensure_session (self ) -> requests .Session :
152+ """Lazily create the HTTP session"""
153+ if self ._session is None :
154+ self ._session = requests .Session ()
155+ return self ._session
156+
141157 def _auth (self ) -> tuple [str , str ] | None :
142158 user = self ._username .resolve_value () if self ._username else None
143159 pwd = self ._password .resolve_value () if self ._password else None
@@ -152,7 +168,7 @@ def _command(self, sql: str, *, positional_params: list[Any] | None = None) -> l
152168 if positional_params :
153169 payload ["params" ] = positional_params
154170
155- resp = self ._session .post (url , json = payload , auth = self ._auth ())
171+ resp = self ._ensure_session () .post (url , json = payload , auth = self ._auth ())
156172 if resp .status_code >= HTTPStatus .BAD_REQUEST :
157173 msg = f"ArcadeDB command failed ({ resp .status_code } ): { resp .text } "
158174 raise RuntimeError (msg )
@@ -163,7 +179,7 @@ def _command(self, sql: str, *, positional_params: list[Any] | None = None) -> l
163179 def _server_command (self , command : str ) -> dict [str , Any ]:
164180 """Execute a server-level command (e.g. CREATE DATABASE)."""
165181 url = f"{ self ._url } /api/v1/server"
166- resp = self ._session .post (url , json = {"command" : command }, auth = self ._auth ())
182+ resp = self ._ensure_session () .post (url , json = {"command" : command }, auth = self ._auth ())
167183 if resp .status_code >= HTTPStatus .BAD_REQUEST :
168184 msg = f"ArcadeDB server command failed ({ resp .status_code } ): { resp .text } "
169185 raise RuntimeError (msg )
0 commit comments