Skip to content

Commit e941865

Browse files
committed
Added close method for Connection Pool
1 parent 2b1ded9 commit e941865

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

python/psqlpy/_internal/__init__.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,12 @@ class PSQLPool:
784784
785785
You must call it before start making queries.
786786
"""
787+
async def close(self: Self) -> None:
788+
"""Close the connection pool.
789+
790+
By default it will be closed automatically,
791+
but you can call it manually.
792+
"""
787793
async def execute(
788794
self: Self,
789795
querystring: str,

src/driver/connection_pool.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,23 @@ impl RustPSQLPool {
183183
self.db_pool = Some(db_pool_builder.build()?);
184184
Ok(())
185185
}
186+
187+
/// Close connection pool.
188+
///
189+
/// # Errors
190+
/// May return Err Result if connection pool isn't opened.
191+
pub fn inner_close(&self) -> RustPSQLDriverPyResult<()> {
192+
let db_pool_manager =
193+
self.db_pool
194+
.as_ref()
195+
.ok_or(RustPSQLDriverError::DatabasePoolError(
196+
"Database pool is not initialized".into(),
197+
))?;
198+
199+
db_pool_manager.close();
200+
201+
Ok(())
202+
}
186203
}
187204

188205
#[pyclass()]
@@ -257,16 +274,30 @@ impl PSQLPool {
257274
querystring: String,
258275
parameters: Option<&'a PyAny>,
259276
) -> RustPSQLDriverPyResult<&'a PyAny> {
260-
let engine_arc = self.rust_psql_pool.clone();
277+
let db_pool_arc = self.rust_psql_pool.clone();
261278
let mut params: Vec<PythonDTO> = vec![];
262279
if let Some(parameters) = parameters {
263280
params = convert_parameters(parameters)?;
264281
}
265282

266283
rustengine_future(py, async move {
267-
let engine_guard = engine_arc.read().await;
284+
let db_pool_guard = db_pool_arc.read().await;
285+
286+
db_pool_guard.inner_execute(querystring, params).await
287+
})
288+
}
289+
290+
/// Close connection pool.
291+
///
292+
/// # Errors
293+
/// May return Err Result if connection pool isn't opened.
294+
pub fn close<'a>(&self, py: Python<'a>) -> RustPSQLDriverPyResult<&'a PyAny> {
295+
let db_pool_arc = self.rust_psql_pool.clone();
296+
297+
rustengine_future(py, async move {
298+
let db_pool_guard = db_pool_arc.read().await;
268299

269-
engine_guard.inner_execute(querystring, params).await
300+
db_pool_guard.inner_close()
270301
})
271302
}
272303
}

0 commit comments

Comments
 (0)