2626from sqlalchemy import select
2727from sqlalchemy .ext .asyncio import AsyncEngine , create_async_engine
2828from sqlalchemy .future .engine import Engine , create_engine
29- from sqlalchemy .orm import Session , sessionmaker
29+ from sqlalchemy .orm import Session
3030
3131from deepchecks_monitoring import config
3232from deepchecks_monitoring .features_control import FeaturesControl
@@ -79,9 +79,7 @@ class ResourcesProvider(BaseResourcesProvider):
7979 def __init__ (self , settings : config .BaseSettings ):
8080 self ._settings = settings
8181 self ._database_engine : t .Optional [Engine ] = None
82- self ._session_factory : t .Optional [sessionmaker ] = None
8382 self ._async_database_engine : t .Optional [AsyncEngine ] = None
84- self ._async_session_factory : t .Optional [sessionmaker ] = None
8583 self ._kafka_admin : t .Optional [KafkaAdminClient ] = None
8684 self ._redis_client : t .Optional [Redis ] = None
8785 self ._cache_funcs : t .Optional [CacheFunctions ] = None
@@ -147,15 +145,11 @@ def settings(self) -> config.Settings:
147145
148146 def dispose_resources (self ):
149147 """Dispose resources."""
150- if self ._session_factory is not None :
151- self ._session_factory .close_all ()
152148 if self ._database_engine is not None :
153149 self ._database_engine .dispose ()
154150
155151 async def async_dispose_resources (self ):
156152 """Dispose async resources."""
157- # if self._async_session_factory is not None:
158- # await AsyncSession.close_all()
159153 if self ._async_database_engine is not None :
160154 await self ._async_database_engine .dispose ()
161155
@@ -180,23 +174,15 @@ def database_engine(self) -> Engine:
180174
181175 return self ._database_engine
182176
183- @property
184- def session_factory (self ) -> sessionmaker :
185- """Return alchemy session factory."""
186- if self ._session_factory is None :
187- self ._session_factory = sessionmaker (
188- self .database_engine ,
189- # class_=ExtendedAsyncSession, # TODO:
190- autoflush = False ,
191- expire_on_commit = False ,
192- autocommit = False ,
193- )
194- return self ._session_factory
195-
196177 @contextmanager
197178 def create_database_session (self ) -> t .Iterator [Session ]:
198179 """Create sqlalchemy database session."""
199- with self .session_factory () as session : # pylint: disable=not-callable
180+ with Session (
181+ self .database_engine ,
182+ autoflush = False ,
183+ expire_on_commit = False ,
184+ autocommit = False ,
185+ ) as session : # pylint: disable=not-callable
200186 try :
201187 yield session
202188 session .commit ()
@@ -225,19 +211,6 @@ def async_database_engine(self) -> AsyncEngine:
225211 )
226212 return self ._async_database_engine
227213
228- @property
229- def async_session_factory (self ) -> sessionmaker :
230- """Return async alchemy session maker."""
231- if self ._async_session_factory is None :
232- self ._async_session_factory = sessionmaker (
233- self .async_database_engine ,
234- class_ = ExtendedAsyncSession ,
235- autoflush = False ,
236- expire_on_commit = False ,
237- autocommit = False ,
238- )
239- return self ._async_session_factory
240-
241214 @t .overload
242215 def create_async_database_session (
243216 self ,
@@ -260,8 +233,12 @@ async def create_async_database_session(
260233 organization_id : t .Optional [int ] = None
261234 ) -> t .AsyncIterator [t .Optional [ExtendedAsyncSession ]]:
262235 """Create async sqlalchemy database session."""
263- async with self .async_session_factory () as session : # pylint: disable=not-callable
264- session : ExtendedAsyncSession
236+ async with ExtendedAsyncSession (
237+ self .async_database_engine ,
238+ autoflush = False ,
239+ expire_on_commit = False ,
240+ autocommit = False ,
241+ ) as session : # pylint: disable=not-callable
265242 try :
266243 if organization_id :
267244 organization_schema = await session .scalar (
0 commit comments