Skip to content

Commit d35788d

Browse files
committed
Revert .close removal
1 parent edf13c6 commit d35788d

File tree

5 files changed

+29
-0
lines changed

5 files changed

+29
-0
lines changed

docs/examples/aiohttp/start_example.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@ async def start_db_pool(app: web.Application) -> None:
1616
app["db_pool"] = db_pool
1717

1818

19+
async def stop_db_pool(app: web.Application) -> None:
20+
"""Close database connection pool."""
21+
db_pool = cast(PSQLPool, app.db_pool)
22+
await db_pool.close()
23+
24+
1925
async def pg_pool_example(request: web.Request) -> Any:
2026
db_pool = cast(PSQLPool, request.app["db_pool"])
2127
connection = await db_pool.connection()

docs/examples/fastapi/advanced_example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
1919
"""Startup database connection pool and close it on shutdown."""
2020
app.state.db_pool = db_pool
2121
yield
22+
await db_pool.close()
2223

2324

2425
app = FastAPI(lifespan=lifespan)

docs/examples/fastapi/start_example.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
1818
)
1919
app.state.db_pool = db_pool
2020
yield
21+
await db_pool.close()
2122

2223

2324
app = FastAPI(lifespan=lifespan)

python/psqlpy/_internal/__init__.pyi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,12 @@ class PSQLPool:
882882
- `max_db_pool_size`: maximum size of the connection pool
883883
- `conn_recycling_method`: how a connection is recycled.
884884
"""
885+
async def close(self: Self) -> None:
886+
"""Close the connection pool.
887+
888+
By default it will be closed automatically,
889+
but you can call it manually.
890+
"""
885891
async def execute(
886892
self: Self,
887893
querystring: str,

python/tests/test_connection_pool.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from psqlpy import Connection, ConnRecyclingMethod, PSQLPool, QueryResult
4+
from psqlpy.exceptions import RustPSQLDriverPyBaseError
45

56
pytestmark = pytest.mark.anyio
67

@@ -56,3 +57,17 @@ async def test_pool_conn_recycling_method(
5657
)
5758

5859
await pg_pool.execute("SELECT 1")
60+
61+
62+
async def test_close_connection_pool() -> None:
63+
"""Test that `close` method closes connection pool."""
64+
pg_pool = PSQLPool(
65+
dsn="postgres://postgres:postgres@localhost:5432/psqlpy_test",
66+
)
67+
68+
await pg_pool.execute("SELECT 1")
69+
70+
await pg_pool.close()
71+
72+
with pytest.raises(expected_exception=RustPSQLDriverPyBaseError):
73+
await pg_pool.execute("SELECT 1")

0 commit comments

Comments
 (0)