diff --git a/databasez/core/database.py b/databasez/core/database.py index 1f564e7..70da951 100644 --- a/databasez/core/database.py +++ b/databasez/core/database.py @@ -449,6 +449,8 @@ async def drop_all(self, meta: MetaData, **kwargs: typing.Any) -> None: @multiloop_protector(False) def connection(self) -> Connection: + if not self.is_connected: + raise RuntimeError("Database is not connected") if self.force_rollback: return typing.cast(Connection, self._global_connection) diff --git a/databasez/sqlalchemy.py b/databasez/sqlalchemy.py index c2cdcfe..75884ed 100644 --- a/databasez/sqlalchemy.py +++ b/databasez/sqlalchemy.py @@ -295,5 +295,6 @@ async def connect(self, database_url: DatabaseURL, **options: typing.Any) -> Non async def disconnect(self) -> None: engine = self.engine + self.engine = None assert engine is not None, "database is not initialized" await engine.dispose()